Skip to content

Instantly share code, notes, and snippets.

@mwean
Created May 31, 2019 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwean/9c928e7ccf88e7b8d48b8813198195b4 to your computer and use it in GitHub Desktop.
Save mwean/9c928e7ccf88e7b8d48b8813198195b4 to your computer and use it in GitHub Desktop.
class Bottles
def song
verses(99, 0)
end
def verses(high, low)
high.downto(low).map { |n| verse(n) }.join("\n")
end
def verse(n)
[line1(n), line2(n)].join("\n") + "\n"
end
private
def line1(n)
"#{bottle_str(n).capitalize} of beer on the wall, #{bottle_str(n)} of beer."
end
def line2(n)
if n > 0
"Take #{it_one(n)} down and pass it around, #{bottle_str(n - 1)} of beer on the wall."
else
"Go to the store and buy some more, 99 bottles of beer on the wall."
end
end
def it_one(n)
n == 1 ? "it" : "one"
end
def count(n)
n > 0 ? n : "no more"
end
def bottle_str(n)
[count(n), bottle(n)].join(" ")
end
def bottle(n)
n == 1 ? "bottle" : "bottles"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment