Skip to content

Instantly share code, notes, and snippets.

@jarhill0
Last active May 17, 2020 18:57
Show Gist options
  • Save jarhill0/b9a8de079aa145120201d052ebd95d8a to your computer and use it in GitHub Desktop.
Save jarhill0/b9a8de079aa145120201d052ebd95d8a to your computer and use it in GitHub Desktop.
My first time writing Ruby — worked for a half hour on the introductory exercise from "99 Bottles of OOP." All tests pass!
class Bottles
def song
verses 99, 0
end
def verses(start, stop)
start.downto(stop).map {|v| self.verse v}.join("\n")
end
def verse(num)
"#{self.bottle_text(num).capitalize} of beer on the wall, #{self.bottle_text num} of beer.
#{self.next_action num}
"
end
def next_action(num)
if num >= 1
"Take #{num == 1 ? "it" : "one"} down and pass it around, #{self.bottle_text (num - 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 bottle_text(num)
if num == 0
"no more bottles"
elsif num == 1
"1 bottle"
else
"#{num} bottles"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment