Skip to content

Instantly share code, notes, and snippets.

@damianryan
Created July 2, 2014 10:16
Show Gist options
  • Save damianryan/35900e6ac965b48e65d5 to your computer and use it in GitHub Desktop.
Save damianryan/35900e6ac965b48e65d5 to your computer and use it in GitHub Desktop.
Nat & Damian's solution to #spaconf_2014
class Bottles
@@bottles = {
0 => 'no more bottles',
1 => '1 bottle',
2 => '{n} bottles'
}
@@actions = {
0 => 'Go to the store and buy some more',
1 => 'Take it down and pass it around',
2 => 'Take one down and pass it around'
}
def initialize
@my_bottles = @@bottles
end
def verse(n)
this_bottles=bottles(n)
next_bottles=bottles((n + 99 ) % 100)
<<-VERSE
#{this_bottles.to_str.capitalize} of beer on the wall, #{this_bottles} of beer.
#{action(n)}, #{next_bottles} of beer on the wall.
VERSE
end
def bottles(n)
phrase(@my_bottles, n)
end
def action(n)
phrase(@@actions, n)
end
def phrase(phrases, n)
(phrases[n] or phrases[2]).sub("{n}", n.to_s)
end
def verses(n1, n2)
(n1.downto n2).map {|n| verse(n)}.join("\n")
end
def song
verses(99, 0)
end
end
class SixPackBottles < Bottles
@@bottles = {
0 => 'no more bottles',
1 => '1 bottle',
2 => '{n} bottles',
6 => '1 six pack'
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment