Forgiving the contrived example, if I have…
class Condiment
def ketchup(quantity)
puts "adding #{quantity} of ketchup!"
end
end
class OverpricedStadiumSnack
def add
Condiment.new
end
end
hotdog = OverpricedStadiumSnack.new
… is there anyway to get access to the hotdog instantiated object from within Condiment#ketchup when calling hotdog.add.ketchup('tons!')??
So far the only solution I’ve found is to pass hotdog in explicitly, like so:
class Condiment
def ketchup(quantity, snack)
puts "adding #{quantity} of ketchup to your #{snack.type}!"
end
end
class OverpricedStadiumSnack
attr_accessor :type
def add
Condiment.new
end
end
hotdog = OverpricedStadiumSnack.new
hotdog.type = 'hotdog'
# call with
hotdog.add.ketchup('tons!', hotdog)
… but I would love to be able to do this without passing hotdog explicitly.
May be: