Skip to content

Instantly share code, notes, and snippets.

@psparrow
Last active February 18, 2017 20:09
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 psparrow/fd34cfb4a7989db2016047d62ab02162 to your computer and use it in GitHub Desktop.
Save psparrow/fd34cfb4a7989db2016047d62ab02162 to your computer and use it in GitHub Desktop.
My solution to Sandi Metz and Katrina Owen's 99 Bottles of OOP problem
class Bottles
def song
verses 99, 0
end
def verses high, low
high.downto(low).map { |count| verse count }.join "\n"
end
def verse count
bottles = Hash.new { |hash, key| "#{key} bottles" }
bottles[6] = '1 six-pack'
bottles[1] = '1 bottle'
bottles[0] = 'no more bottles'
actions = Hash.new 'Take one down and pass it around'
actions[1] = 'Take it down and pass it around'
actions[0] = 'Go to the store and buy some more'
remaining = count > 0 ? count - 1 : 99
<<-VERSE
#{bottles[count].capitalize} of beer on the wall, #{bottles[count]} of beer.
#{actions[count]}, #{bottles[remaining]} of beer on the wall.
VERSE
end
end
@psparrow
Copy link
Author

Replacing "6 bottles" with "1 six-pack" only requires adding a single line of code...

bottles[6] = '1 six-pack'

@psparrow
Copy link
Author

Updating this with a minimal refactoring and the addition of the six-pack feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment