instance_exec vs instance_eval

instance_exec allows you pass arguments to the block which is to be evaluated in the new self:

o = Struct.new(:val).new("Hello")
o.instance_exec("World!"){|arg| val + " " + arg}      # => Hello World!
 reference: http://eigenclass.org/hiki.rb?instance_exec

你可能感兴趣的:(instance)