Created
March 30, 2017 16:47
-
-
Save jsrn/403b232ad3fabd1cd8ba986fd383a12c to your computer and use it in GitHub Desktop.
bottles
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 verse(verse_number) | |
return final_verse if verse_number == 1 | |
return empty_verse if verse_number == 0 | |
normal_verse(verse_number) | |
end | |
def verses(max, min) | |
max.downto(min).map { |verse| Bottles.new.verse(verse) }.join("\n") | |
end | |
def song | |
Bottles.new.verses(99, 0) | |
end | |
private | |
def normal_verse(verse_number) | |
<<-VERSE | |
#{verse_number} #{pluralize_bottle(verse_number)} of beer on the wall, #{verse_number} #{pluralize_bottle(verse_number)} of beer. | |
Take one down and pass it around, #{verse_number - 1} #{pluralize_bottle(verse_number -1)} of beer on the wall. | |
VERSE | |
end | |
def final_verse | |
<<-VERSE | |
1 bottle of beer on the wall, 1 bottle of beer. | |
Take it down and pass it around, no more bottles of beer on the wall. | |
VERSE | |
end | |
def empty_verse | |
<<-VERSE | |
No more bottles of beer on the wall, no more bottles of beer. | |
Go to the store and buy some more, 99 bottles of beer on the wall. | |
VERSE | |
end | |
def pluralize_bottle(count) | |
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