class Food
def initialize(base)
@base = base
end
def topping=(value)
if compatible?(@base, value)
@topping = value
return true
else
return false
end
end
end
def compatible?(base, topping)
case base
when :Pizza then [:Cheese, :Garlic, :Spinach].include? topping
when :IceCream then [:ChocolateSauce, :Sprinkles].include? topping
end
end
icecream = Food.new :IceCream
[:ChocolateSauce, :Sprinkles, :Garlic].each do |topping|
if icecream.topping = topping
puts 'Mmmhh! :-)'
else
puts 'Umm... better not.'
end
end
Why is the code printing 'Mmmhh! :-)' when I put garlic on my ice cream?
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.