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
| def weather(sym) | |
| {:san_francisco => 60, :chicago => 30, :miami => 70, :los_angeles => 80, :anchorage => 10}[sym] | |
| 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 Numeric | |
| def factorial() | |
| self < 0 ? raise("You can't take the factorial of a negative number") : factorial_h(1,self) | |
| end | |
| def factorial_h(accum, n) | |
| n <= 1 ? accum : factorial_h(accum * n, n -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 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] |
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 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
| 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 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 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 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 Array | |
| def new_collect | |
| ar = [] | |
| self.each do |i| | |
| ar << yield(i) | |
| end | |
| ar | |
| end | |
| end |