Skip to content

Instantly share code, notes, and snippets.

@Ebtoulson
Created May 22, 2014 14:01
Show Gist options
  • Save Ebtoulson/cfe625e9f115d66963e7 to your computer and use it in GitHub Desktop.
Save Ebtoulson/cfe625e9f115d66963e7 to your computer and use it in GitHub Desktop.
class Bottles
attr_reader :where, :substance
def initialize(substance='of beer', where='on the wall')
@substance = substance
@where = where
end
def verse bottle_number
next_bottle_number = bottle_number - 1
next_bottle_number = 99 if next_bottle_number == -1
"#{how_many(bottle_number).capitalize} #{container bottle_number} #{substance} #{where}, " +
"#{how_many bottle_number} #{container bottle_number} #{substance}.\n" +
"#{what_to_do bottle_number} #{how_many(next_bottle_number)} #{container(next_bottle_number)} " +
"#{substance} #{where}.\n"
end
def verses begin_number, end_number
begin_number.downto(end_number).map do |n|
verse(n)
end.join("\n")
end
def song
starting_quanity = 99
ending_quanity = 0
verses(starting_quanity, ending_quanity)
end
private
def container quantity
case quantity
when 1
"bottle"
when 6
"sixpack"
else
"bottles"
end
end
def pronoun quantity
quantity == 1 ? "it" : "one"
end
def how_many quantity
case quantity
when 0
"no more"
when 6
"a"
else
quantity.to_s
end
end
def what_to_do quantity
if quantity == 0
"Go to the store and buy some more,"
else
"Take #{pronoun quantity} down and pass it around,"
end
end
end
class SixPack < Bottles
def container quantity
case quantity
when 6
"sixpack"
else
super(quantity)
end
end
def how_many quantity
case quantity
when 6
"a"
else
super(quantity)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment