Last active
September 8, 2017 20:12
-
-
Save flanger001/64f504111db0e98b73c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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." |
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.
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
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?