Skip to content

Instantly share code, notes, and snippets.

@flanger001
Last active September 8, 2017 20:12
Show Gist options
  • Save flanger001/64f504111db0e98b73c7 to your computer and use it in GitHub Desktop.
Save flanger001/64f504111db0e98b73c7 to your computer and use it in GitHub Desktop.
# Now more objecty
class PotOfCoffee
attr_reader :scoops
def initialize(quantity: 12, strength: :normal)
@quantity = quantity
@strength = strength
end
def ratio
@ratio = { weak: 0.5, normal: 0.75, extra: 0.875, turbo: 1, omg: 2, wtf: 3.8 }
end
def scoops
quantity * ratio.fetch(strength)
rescue KeyError
"I don't know how to make #{strength} strength coffee, sorry."
end
end
pot = PotOfCoffee.new
puts pot.scoops # 9.0
extra_pot = PotOfCoffee.new(quantity: 8, strength: :extra)
puts extra_pot.scoops # 7.0
wtf_pot = PotOfCoffee.new(quantity: 12, strength: :wtf)
puts wtf_pot.scoops # 45.599999999999994
error_pot = PotOfCoffee.new(quantity: 12, strength: 'asdgas')
puts error_pot.scoops # "I don't know how to make asdgas strength coffee, sorry."
@tibbon
Copy link

tibbon commented Feb 9, 2015

I think you're too tightly coupling the concept of a Scoop and a Pot. They are clearly different objects with different behaviors.

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