Created
October 30, 2014 19:37
-
-
Save bgreg/094b854f53b88f17a2a4 to your computer and use it in GitHub Desktop.
This file contains 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 Bottles | |
def song | |
verses(99, 0) | |
end | |
def verses(start, finish) | |
start.downto(finish).map {|i| verse(i)}.join("\n") | |
end | |
def verse(number) | |
count = BottleCount.new(number) | |
next_count = BottleCount.new(count.remainder) | |
"#{count.name.capitalize} #{count.unit} of beer on the wall,"\ | |
" #{count.name} #{count.unit} of beer.\n#{count.action}, "\ | |
"#{next_count.name} #{next_count.unit} of beer on the wall.\n" | |
end | |
end | |
class BottleCount | |
attr_reader :count | |
def initialize(count) | |
@count = count | |
end | |
def action | |
if count == 0 | |
"Go to the store and buy some more" | |
else | |
"Take #{pronoun} down and pass it around" | |
end | |
end | |
def name | |
if count == 0 | |
"no more" | |
else | |
count.to_s | |
end | |
end | |
def pronoun | |
if count == 1 | |
'it' | |
else | |
'one' | |
end | |
end | |
def remainder | |
if count == 0 | |
99 | |
else | |
count - 1 | |
end | |
end | |
def unit | |
if count == 1 | |
'bottle' | |
else | |
"bottles" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment