Skip to content

Instantly share code, notes, and snippets.

@kaborso
Created May 22, 2014 13:59
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 kaborso/247d368f659b1c136bad to your computer and use it in GitHub Desktop.
Save kaborso/247d368f659b1c136bad to your computer and use it in GitHub Desktop.
beginning of bottles song implementation
class BottleSong
def initialize
@type = 'bottles'
end
def song
verses(99, 0)
end
def verses(start, the_end)
start.downto(the_end).map {|i| verse(i)}.join("\n")
end
def verse(num)
"#{how_many(num)} #{containers(num)} of beer on the wall, ".capitalize +
"#{how_many(num)} #{containers(num)} of beer.\n" +
"#{what_to_do(num)}, " +
"#{how_many(num-1)} #{containers(num-1)} of beer on the wall.\n"
end
private
def what_to_do(num)
if num.zero?
"Go to the store and buy some more"
else
"Take #{pronoun(num)} down and pass it around"
end
end
def how_many(num)
if num.zero?
'no more'
elsif num < 0
"99"
else
"#{num}"
end
end
def pronoun(num)
if num == 1
'it'
else
'one'
end
end
def containers(num)
if num == 1
'bottle'
else
'bottles'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment