【发布时间】:2010-11-28 13:03:41
【问题描述】:
class Foo
def with_yield
yield(self)
end
def with_instance_eval(&block)
instance_eval(&block)
end
end
f = Foo.new
f.with_yield do |arg|
p self
# => main
p arg
# => #<Foo:0x100124b10>
end
f.with_instance_eval do |arg|
p self
# => #<Foo:0x100124b10>
p arg
# => #<Foo:0x100124b10>
end
为什么第二个 'p arg' 报告 Foo 实例?它不应该报告nil,因为with_instance_eval 不会产生self 到块?
【问题讨论】:
-
哦,嘿,这是我来自 stackoverflow.com/questions/1425055/… 的代码 :)
标签: ruby instance-eval