Skip to content

Instantly share code, notes, and snippets.

@wjessop
Last active May 19, 2017 13:16
Show Gist options
  • Save wjessop/32fa2e097c979798e8bbf7ad57132775 to your computer and use it in GitHub Desktop.
Save wjessop/32fa2e097c979798e8bbf7ad57132775 to your computer and use it in GitHub Desktop.
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
@ciaran
Copy link

ciaran commented May 19, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment