Last active
May 19, 2017 13:16
-
-
Save wjessop/32fa2e097c979798e8bbf7ad57132775 to your computer and use it in GitHub Desktop.
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 verses(start, last) | |
start.downto(last).map.map{|v| verse(v) }.join("\n") | |
end | |
def verse(num_bottles) | |
"#{bottles_of_beer_on_the_wall(num_bottles).capitalize} of beer on the wall, #{bottles_of_beer_on_the_wall(num_bottles)} of beer. | |
#{action(num_bottles)}. | |
" | |
end | |
def song | |
verses(99, 0) | |
end | |
private | |
def action(n) | |
if n == 0 | |
"Go to the store and buy some more, 99 bottles of beer on the wall" | |
else | |
"Take #{it_or_one(n)} down and pass it around, #{bottles_left(n)} of beer on the wall" | |
end | |
end | |
def pluralise_bottle(n) | |
n < 2 ? "bottle" : "bottles" | |
end | |
def it_or_one(n) | |
n == 1 ? "it" : "one" | |
end | |
def bottles_of_beer_on_the_wall(n) | |
if n < 1 | |
"no more bottles" | |
else | |
"#{n} #{pluralise_bottle(n)}" | |
end | |
end | |
def bottles_left(n) | |
if n > 2 | |
"#{n-1} bottles" | |
elsif n == 1 | |
"no more bottles" | |
else | |
"1 bottle" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's this in Elixir for some reason: https://gist.github.com/ciaran/097dec82cf85d849954d30269d23504c