Skip to content

Instantly share code, notes, and snippets.

@Thomascountz
Last active June 12, 2018 12:52
Show Gist options
  • Save Thomascountz/64c95552f4a8b1dd4673d0a093036390 to your computer and use it in GitHub Desktop.
Save Thomascountz/64c95552f4a8b1dd4673d0a093036390 to your computer and use it in GitHub Desktop.
Listing 6.5 - 99 Bottles of OOP
# https://github.com/sandimetz/99bottles/blob/862f37346dfa8a9630c2ace0423e59c207a9f3f8/lib/bottles.rb
class BottleNumber
attr_reader :number
def initialize(number)
@number = number
end
def to_s
"#{quantity} #{container}"
end
def container
if number == 1
"bottle"
else
"bottles"
end
end
def quantity
if number == 0
"no more"
else
number.to_s
end
end
def action
if number == 0
"Go to the store and buy some more"
else
"Take #{pronoun} down and pass it around"
end
end
def pronoun
if number == 1
"it"
else
"one"
end
end
def successor
if number == 0
99
else
number - 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment