diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-05-26 03:12:58 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-05-26 03:12:58 +0000 |
commit | c46c73c7a287f7bb16e004a9838e724540214b63 (patch) | |
tree | 1e9c868ada4358ab93844140ac2c360323398296 /eval.c | |
parent | 35ec34fa2f20e208e2d1ad5574c7d81a8ae9ecc3 (diff) |
Merge from ruby_1_8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_7@16606 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'eval.c')
-rw-r--r-- | eval.c | 60 |
1 files changed, 56 insertions, 4 deletions
@@ -6746,12 +6746,28 @@ yield_under_i(self) return rb_yield_0(self, self, ruby_class, YIELD_PUBLIC_DEF, Qfalse); } +static VALUE +yield_args_under_i(vinfo) + VALUE vinfo; +{ + VALUE *info = (VALUE *)vinfo; + + return rb_yield_0(info[0], info[1], ruby_class, YIELD_PUBLIC_DEF, Qtrue); +} + /* block eval under the class/module context */ static VALUE -yield_under(under, self) - VALUE under, self; +yield_under(under, self, args) + VALUE under, self, args; { - return exec_under(yield_under_i, under, 0, self); + if (args == Qundef) { + return exec_under(yield_under_i, under, 0, self); + } + else { + VALUE info[2] = { args, self }; + + return exec_under(yield_args_under_i, under, 0, (VALUE)info); + } } static VALUE @@ -6764,7 +6780,7 @@ specific_eval(argc, argv, klass, self) if (argc > 0) { rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc); } - return yield_under(klass, self); + return yield_under(klass, self, Qundef); } else { char *file = "(eval)"; @@ -6836,6 +6852,41 @@ rb_obj_instance_eval(argc, argv, self) /* * 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)); +} + +/* + * call-seq: * mod.class_eval(string [, filename [, lineno]]) => obj * mod.module_eval {|| block } => obj * @@ -8120,6 +8171,7 @@ Init_eval() rb_define_method(rb_mKernel, "send", rb_f_send, -1); rb_define_method(rb_mKernel, "__send__", rb_f_send, -1); rb_define_method(rb_mKernel, "instance_eval", rb_obj_instance_eval, -1); + rb_define_method(rb_mKernel, "instance_exec", rb_obj_instance_exec, -1); rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1); rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1); |