原谅人为的例子,如果我有......
class Condiment
def ketchup(quantity)
puts "adding #{quantity} of ketchup!"
end
end
class OverpricedStadiumSnack
def add
Condiment.new
end
end
hotdog = OverpricedStadiumSnack.new
...调用时是否可以从内部访问hotdog
实例化对象?Condiment#ketchup
hotdog.add.ketchup('tons!')
到目前为止,我发现的唯一解决方案是hotdog
显式传递,如下所示:
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)
...但我希望能够在不hotdog
明确传递的情况下做到这一点。