Created
July 31, 2016 15:59
-
-
Save francirp/dfdcd7f19d3d41baea0e16e61bcfbc9e to your computer and use it in GitHub Desktop.
My 99bottles solution
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 | |
attr_reader :bottle_number | |
def verse(bottle_number) | |
@bottle_number = bottle_number | |
return plural if bottle_number > 2 | |
return two if bottle_number == 2 | |
return one if bottle_number == 1 | |
return zero if bottle_number == 0 | |
end | |
def verses(first_bottle, last_bottle) | |
result = (last_bottle..first_bottle).to_a.reverse.map do |num| | |
verse(num) | |
end.join("\n") | |
result | |
end | |
def song | |
verses(99, 0) | |
end | |
private | |
def plural | |
first = "#{bottle_number} bottles of beer on the wall, #{bottle_number} bottles of beer.\n" | |
second = "Take one down and pass it around, #{next_num} bottles of beer on the wall.\n" | |
[first, second].join('') | |
end | |
def two | |
first = "2 bottles of beer on the wall, 2 bottles of beer.\n" | |
second = "Take one down and pass it around, 1 bottle of beer on the wall.\n" | |
[first, second].join('') | |
end | |
def one | |
first = "1 bottle of beer on the wall, 1 bottle of beer.\n" | |
second = "Take it down and pass it around, no more bottles of beer on the wall.\n" | |
[first, second].join('') | |
end | |
def zero | |
first = "No more bottles of beer on the wall, no more bottles of beer.\n" | |
second = "Go to the store and buy some more, 99 bottles of beer on the wall.\n" | |
[first, second].join('') | |
end | |
def next_num | |
bottle_number - 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment