Created
July 2, 2014 10:32
-
-
Save mbateman/539219d71635d108f79d 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 initialize | |
@verses = { | |
2 => LastButOneVerse::new, | |
1 => LastVerse::new, | |
0 => EpilogueVerse::new | |
} | |
3.upto(99) do |index| | |
@verses[index] = StandardVerse::new("#{index} bottles of beer", | |
"#{index-1} bottles of beer") | |
end | |
@verses[6] = StandardVerse::new("1 six-pack of beer", | |
"5 bottles of beer") | |
end | |
def verse(line) | |
return @verses[line].verse() | |
end | |
def song | |
return verses(99, 0) | |
end | |
def verses(from, to) | |
from.downto(to).map{|index| verse(index)}.join("\n") | |
end | |
end | |
def format_verse(on_the_wall, action, remaining) | |
"#{on_the_wall.capitalize} on the wall, #{on_the_wall}.\n" + | |
"#{action}, #{remaining} on the wall.\n" | |
end | |
class StandardVerse | |
def initialize(on_the_wall, remaining) | |
@on_the_wall = on_the_wall | |
@remaining = remaining | |
end | |
def verse | |
format_verse( | |
@on_the_wall, | |
"Take one down and pass it around", | |
@remaining) | |
end | |
end | |
class LastButOneVerse | |
def verse | |
format_verse( | |
"2 bottles of beer", | |
"Take one down and pass it around", | |
"1 bottle of beer") | |
end | |
end | |
class LastVerse | |
def verse | |
format_verse( | |
"1 bottle of beer", | |
"Take it down and pass it around", | |
"no more bottles of beer") | |
end | |
end | |
class EpilogueVerse | |
def verse | |
format_verse( | |
"no more bottles of beer", | |
"Go to the store and buy some more", | |
"99 bottles of beer") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment