Last active
December 20, 2015 08:59
-
-
Save steveklabnik/6104849 to your computer and use it in GitHub Desktop.
99 bottles of metaprogrammed beer
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 Beer | |
def sing(start, finish = 0) | |
start.downto(finish).collect do |number| | |
verse(number) + "\n" | |
end.join | |
end | |
def verse(number) | |
Kernel.const_get("Beer::Verse#{number}") | |
end | |
99.downto(3) do |number| | |
Kernel.const_set("Verse#{number}", | |
"#{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 | |
Verse2 = "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" | |
Verse1 = "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" | |
Verse0 = "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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment