Skip to content

Instantly share code, notes, and snippets.

@mctaylorpants
Created December 8, 2016 17:30
Show Gist options
  • Save mctaylorpants/5b38f679b7e63c026c27f4c2b1c470f1 to your computer and use it in GitHub Desktop.
Save mctaylorpants/5b38f679b7e63c026c27f4c2b1c470f1 to your computer and use it in GitHub Desktop.
99 Bottles Solution 1
class Bottles
def song
verses(99, 0)
end
def verses(*verses)
output = (verses[1]..verses[0]).to_a.reverse.map { |n| verse(n) }
output.join("\n")
end
def verse(num)
new_qty = if (num - 1 == 1)
"1 bottle"
else
"#{num - 1} bottles"
end
case num
when 0
<<-VERSE_0
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_0
when 1
<<-VERSE_1
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_1
else
<<-VERSE_X
#{num} bottles of beer on the wall, #{num} bottles of beer.
Take one down and pass it around, #{new_qty} of beer on the wall.
VERSE_X
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment