Skip to content

Instantly share code, notes, and snippets.

@sundhine
Created July 2, 2014 10:11
Show Gist options
  • Save sundhine/f29a88f50fa834de7773 to your computer and use it in GitHub Desktop.
Save sundhine/f29a88f50fa834de7773 to your computer and use it in GitHub Desktop.
require 'unicode'
class Bottles
class Verse
end
def verses(numBottlesFrom, numBottlesTo)
result = ""
for num in numBottlesFrom.downto(numBottlesTo)
result = result + verse(num)
if (num != numBottlesTo)
result = result + "\n"
end
end
result
end
def verse(numBottles)
thisBottle = getBottles(numBottles)
activeNumber = activeNumber(numBottles)
nextBottle = getBottles(numBottles - 1)
firstLine = "#{Unicode::capitalize(thisBottle)} of beer on the wall, #{thisBottle} of beer.\n"
if (numBottles > 0)
firstLine + "Take #{activeNumber} down and pass it around, #{nextBottle} of beer on the wall.\n"
else
firstLine + "Go to the store and buy some more, 99 bottles of beer on the wall.\n"
end
end
private
def getBottles(n)
if n == 6
"1 six-pack"
else
"#{getNumber(n)} #{getBottle(n)}"
end
end
def getNumber(n)
if n == 0
return "no more"
else
return n
end
end
def getBottle(n)
if n == 1
return "bottle"
else
return "bottles"
end
end
def activeNumber(n)
if (n == 1)
return "it"
else
return "one"
end
end
public
def song
verses(99,0)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment