我想叫一个lambda改变它的自我价值。为了寻找它,我来到了
instance_exec
,本该完成任务的。
然而,经过1个多小时的学习,我发现这个行为我无法解释:
class Dummy
class << self
attr_accessor :my_lambda
end
self.my_lambda = proc { self }
def test
self.class.my_lambda.call
end
def test1
instance_eval(&self.class.my_lambda)
end
def test2
instance_eval { self.class.my_lambda.call }
end
end
Dummy.new.test
=> Dummy # I was expecting this, returns the class, lexical scope (where `self` was defined)
Dummy.new.test1
=> #<Dummy:0x00007fe982f8b678> # I was expecting this, `self` changed to the receiver of `instance_eval`, which is the instance, so `self` returned the instance.
Dummy.new.test2
=> Dummy # I can't understand this, at all. Why isn't it the same as test1?
那么,为什么我们从中得到不同的结果呢
instance_eval(&self.class.my_lambda)
与
instance_eval { self.class.my_lambda.call }
?
另外,既然我们现在的主题是叫Lambdas。。。如果我将类级别变量更改为:
self.my_lambda = lambda { self }
那么虚拟新试验1引发异常
ArgumentError: wrong number of arguments (given 1, expected 0)
实例评估(&U);自我类我的lambda)