This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Array | |
| def new_count | |
| self.inject(0) { |accum , i| yield(i) ? accum += 1 : accum } | |
| end | |
| end | |
| # Exercise for the reader: Figure out why my original idea: "self.inject(0) { |accum , i| accum += 1 if yield(i) }" is not completely valid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Array | |
| def new_each | |
| 0.upto(self.length-1) do |index| | |
| yield(self[index]) | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Array | |
| def new_collect | |
| ar = [] | |
| self.each do |i| | |
| ar << yield(i) | |
| end | |
| ar | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RPNCalculator | |
| def initialize() | |
| @num_store = [] | |
| end | |
| def push(n) | |
| @num_store << n | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Book | |
| attr_reader :title | |
| def initialize() | |
| @title = "" | |
| end | |
| def title=(str) | |
| accum = [] | |
| str.split.each_with_index do |word, indx| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Dictionary | |
| attr_accessor :entries, :keywords | |
| def initialize() | |
| @entries = {} | |
| @keywords = [] | |
| end | |
| def add(input) | |
| if input.is_a?(String) | |
| str = input |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Die | |
| attr_accessor :sides | |
| def initialize(sides) | |
| @sides = sides | |
| end | |
| def roll | |
| rand(sides) + 1 | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Rectangle | |
| def initialize(length, width) | |
| @length = length | |
| @width = width | |
| end | |
| def perimeter | |
| (@length + @width) * 2 | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module InWords | |
| def in_words() | |
| if self == 0 | |
| return "zero" | |
| end | |
| lvl_array = %w{ not_called thousand million billion trillion } | |
| max_lvl = (self.to_s.length - 1) / 3 | |
| n = self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Numeric | |
| def fibonacci() | |
| if self < 0 | |
| raise "Cannot Fibonacci a negative number" | |
| elsif [0,1].include? self | |
| self | |
| else | |
| ar = [0,1] | |
| 2.upto(self) do |n| | |
| ar << ar[0] + ar[1] |