ruby - when calling instance_eval(&lambda) to pass current context got error 'wrong number of arguments' -
to clear - code running - code proc
but if instead change proc.new lambda, i'm getting error
argumenterror: wrong number of arguments (1 0)
may because instance_eval wants pass self param, , lambda treats method , not accept unknown params?
there 2 examples - first 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 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
you correct in assumption. self being passed proc
, lambda being instance_eval
'ed. major difference between procs , lambdas lambdas check arity of block being being passed them.
so:
class rule def get_rule lambda { |s| puts s.inspect; puts name; } end end p = person.new p.init_rule #<person:0x007fd1099f53d0 @name="ruby"> ruby
here told lambda expect block arity 1 , see in argument inspection, argument indeed self
instance of person class.
Comments
Post a Comment