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 sum | |
| answer = 0 | |
| self.each do |number| | |
| answer = answer + number | |
| end | |
| answer | |
| end | |
| def square |
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 | |
| attr_accessor :calculator | |
| def initialize | |
| @calculator = [] | |
| end | |
| def push (number) | |
| @calculator << number | |
| 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 | |
| def initialize | |
| @dictionary = {} | |
| end | |
| def entries | |
| @dictionary | |
| end | |
| def add (entry) |
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 Timer | |
| def initialize | |
| @seconds = 0 | |
| end | |
| def seconds= (seconds) | |
| @seconds = seconds | |
| 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_accessor :title | |
| def title= (title) | |
| do_not_capitalize = %w{the and a an in of} | |
| answer = [] | |
| title.split(' ').each do |word| | |
| do_not_capitalize.include?(word) ? word = word.downcase : word = word.capitalize | |
| word == "i" ? word = word.capitalize : word | |
| answer << word |
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 Temperature | |
| def initialize (input) | |
| @temperature = input | |
| end | |
| def ftoc (number) | |
| number * 9/5.to_f + 32 | |
| end | |
| def ctof (number) |
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 Friend | |
| def greeting(person = nil) | |
| person.nil? ? "Hello!" : "Hello, #{person}!" | |
| 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
| def measure number=1, &someproc | |
| start_time = Time.now | |
| number.times {someproc.call} | |
| run_time = (Time.now - start_time) / number | |
| 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
| def reverser &proc | |
| answer = [] | |
| proc.call.split(' ').each do |word| | |
| answer<<word.reverse | |
| end | |
| answer.join(' ') | |
| end | |
| def adder number=1, &proc | |
| number + proc.call |
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 translate (phrase) | |
| vowels = %w{a e i o u} | |
| words = phrase.split(' ') | |
| first_letter_of_phrase = phrase.scan(/./).first | |
| new_phrase = [] | |
| words.each do |word| | |
| word = word.downcase | |
| first_letter = word.scan(/./).first |