Reputation: 2470
To be clear - this code is running perfectly - code with proc
but if instead I change Proc.new to lambda, I'm getting an error
ArgumentError: wrong number of arguments (1 for 0)
May be this is because instance_eval wants to pass self as a param, and lambda treats as a method and do not accept unknown params?
There is two examples - first is working:
class Rule
def get_rule
Proc.new { puts name }
end
end
class Person
attr_accessor :name
def init_rule
@name = "ruby"
instance_eval(&Rule.new.get_rule)
end
end
second is not:
class Rule
def get_rule
lambda { puts name }
end
end
class Person
attr_accessor :name
def init_rule
@name = "ruby"
instance_eval(&Rule.new.get_rule)
end
end
Thanks
Upvotes: 6
Views: 1766
Reputation: 6856
You are actually correct in your assumption. Self is being passed to the Proc
and to the lambda as it is being instance_eval
'ed. A major difference between Procs and lambdas is that lambdas check the arity of the block being being passed to them.
So:
class Rule
def get_rule
lambda { |s| puts s.inspect; puts name; }
end
end
class Person
attr_accessor :name
def init_rule
@name = "ruby"
instance_eval(&Rule.new.get_rule)
end
end
p = Person.new
p.init_rule
#<Person:0x007fd1099f53d0 @name="ruby">
ruby
Here I told the lambda to expect a block with arity 1 and as you see in the argument inspection, the argument is indeed the self
instance of Person class.
Upvotes: 4