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 KlassWithSecretdef initialize@secret=99endendk=KlassWithSecret.newk.instance_exec(5){|x|@secret+x}#=> 104
/*
* call-seq:
* obj.instance_exec(arg...) {|var...| block } => obj
*
* 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
*/
VALUE
rb_obj_instance_exec(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE klass;
if (SPECIAL_CONST_P(self)) {
klass = Qnil;
}
else {
klass = rb_singleton_class(self);
}
return yield_under(klass, self, rb_ary_new4(argc, argv));
}