Method: BasicObject#instance_exec

Defined in:
vm_eval.c

#instance_exec(arg...) {|var...| ... } ⇒ Object

Executes the given block within the context of the receiver (obj). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj’s instance variables. Arguments are passed as block parameters.

class KlassWithSecret
  def initialize
    @secret = 99
  end
end
k = KlassWithSecret.new
k.instance_exec(5) {|x| @secret+x }   #=> 104

Yields:

  • (var...)

Returns:

[View source]

2321
2322
2323
2324
2325
# File 'vm_eval.c', line 2321

static VALUE
rb_obj_instance_exec_internal(int argc, const VALUE *argv, VALUE self)
{
    return yield_under(self, TRUE, argc, argv, RB_PASS_CALLED_KEYWORDS);
}