Skip to content

Instantly share code, notes, and snippets.

@mbateman
Created July 2, 2014 10:32
Show Gist options
  • Save mbateman/539219d71635d108f79d to your computer and use it in GitHub Desktop.
Save mbateman/539219d71635d108f79d to your computer and use it in GitHub Desktop.
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