我有以下课程:
class User code1 = Proc.new { } code2 = lambda { } define_method :test do self.class.instance_eval &code1 self.class.instance_eval &code2 end end User.new.test
解决方法
instance_eval正在向lambda产生self(User). Lambdas特别关注它们的参数 – 与方法相同 – 并且如果太少/很多则会引发ArgumentError.
class User code1 = Proc.new { |x| x == User } # true code2 = lambda { |x| x == User } # true define_method :test do self.class.instance_eval &code1 self.class.instance_eval &code2 end end
相关:What’s the difference between a proc and a lambda in Ruby?