Created
December 6, 2013 16:31
-
-
Save danielmurphy/7827772 to your computer and use it in GitHub Desktop.
A solution to the 99 Bottles Song
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 BottlesSong | |
def sing | |
verses(99, 0) | |
end | |
def verses(upper_bound, lower_bound) | |
upper_bound.downto(lower_bound).collect {|n| verse(n) + "\n"}.join | |
end | |
def verse(number) | |
BottleSongVerse.for(number).to_s | |
end | |
end | |
class BottleSongVerse | |
attr_reader :number | |
def self.for(number) | |
begin | |
Object.const_get("BottleSongVerse#{number}") | |
rescue NameError | |
self | |
end.new(number) | |
end | |
def initialize(number) | |
@number = number | |
end | |
def to_s | |
"#{number} bottles of beer on the wall, #{number} bottles of beer.\nTake one down and pass it around, #{number-1} bottles of beer on the wall.\n" | |
end | |
end | |
class BottleSongVerse0 < BottleSongVerse | |
def to_s | |
"No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n" | |
end | |
end | |
class BottleSongVerse1 < BottleSongVerse | |
def to_s | |
"1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n" | |
end | |
end | |
class BottleSongVerse2 < BottleSongVerse | |
def to_s | |
"2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment