Skip to content

Instantly share code, notes, and snippets.

@flanger001
Last active September 8, 2017 20:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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."
@dantswain
Copy link

Can you make this more object oriented? How would that affect its reusability?

Should coffee_ratio really be a method?

Should coffee_mix print out the results or return a string so the caller can do as they will? What would you do if you wanted to use the number in a differently-formatted output?

@flanger001
Copy link
Author

Object-orientation - sure. I revised it.
coffee_ratio - I was looking at it like a variable but was thinking of it like I'd use it in a class. In the first case no, in the new case yes.
coffee_mix - As I was using it before it was just a "talky" function, here it is actually a usable data source.

@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