Skip to content

Instantly share code, notes, and snippets.

@psparrow
Last active Feb 18, 2017
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@psparrow psparrow commented Feb 18, 2017

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

bottles[6] = '1 six-pack'
@psparrow

This comment has been minimized.

Copy link
Owner Author

@psparrow psparrow commented Feb 18, 2017

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