Skip to content

Instantly share code, notes, and snippets.

@tgaeta
Last active September 17, 2023 01:39
Show Gist options
  • Save tgaeta/5571256bb5b44d4ef44e78c1250d0658 to your computer and use it in GitHub Desktop.
Save tgaeta/5571256bb5b44d4ef44e78c1250d0658 to your computer and use it in GitHub Desktop.
99bottles.rb
class Bottles
def song
verses(99, 0)
end
def verse(n)
"#{start(n)} #{pluralization(n)} of beer on the wall, "\
"#{start(n).downcase} #{pluralization(n)} of beer.\n" + beer_chore(n)
end
def verses(from, to)
(to..from).to_a.reverse.map { |n| verse(n) }.join("\n")
end
def pluralization(n)
n == 1 ? 'bottle' : 'bottles'
end
def pronoun(n)
n == 1 ? 'it' : 'one'
end
def remaining(n)
(n - 1).zero? ? 'no more' : n - 1
end
def start(n)
n.zero? ? 'No more' : n.to_s
end
def beer_chore(n)
if remaining(n) == -1
"Go to the store and buy some more, 99 bottles of beer on the wall.\n"
else
"Take #{pronoun(n)} down and pass it around, #{remaining(n)} "\
"#{pluralization(n - 1)} of beer on the wall.\n"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment