Created
March 25, 2019 12:49
-
-
Save JiggyPete/407ed75760225e8930fe51b885010d32 to your computer and use it in GitHub Desktop.
First exercise of the 99 Bottles of Milk book
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 Bottles | |
def song | |
verses(99, 0) | |
end | |
def verses(start_index, end_index) | |
numbers_of_bottles = (end_index..start_index).to_a.reverse | |
all_verses = numbers_of_bottles.map{|number_of_bottles| verse(number_of_bottles)} | |
all_verses.join("\n") | |
end | |
def verse(number_of_bottles) | |
return no_bottles_verse if number_of_bottles == 0 | |
return one_bottles_verse if number_of_bottles == 1 | |
return two_bottles_verse if number_of_bottles == 2 | |
multiple_bottles_verse(number_of_bottles) | |
end | |
private | |
def multiple_bottles_verse(number_of_bottles) | |
<<-VERSE | |
#{number_of_bottles} bottles of beer on the wall, #{number_of_bottles} bottles of beer. | |
Take one down and pass it around, #{number_of_bottles - 1} bottles of beer on the wall. | |
VERSE | |
end | |
def two_bottles_verse | |
<<-VERSE | |
2 bottles of beer on the wall, 2 bottles of beer. | |
Take one down and pass it around, 1 bottle of beer on the wall. | |
VERSE | |
end | |
def one_bottles_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 no_bottles_verse | |
expected = <<-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 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment