1
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
」報告中的富實例?難道不應該報告因with_instance_eval
nil
不會產生self
到塊?爲什麼第二個'p arg'報告Foo實例?
嘿,那是從http://stackoverflow.com/questions/1425055/is-yield-self-the-same-as-instanceeval/1425600#1425600我的代碼:) – 2009-09-16 06:51:11