yarv-diff:257
From: ko1 atdot.net
Date: 15 Feb 2006 05:27:52 -0000
Subject: [yarv-diff:257] r420 - trunk
Author: matz
Date: 2006-02-15 14:27:51 +0900 (Wed, 15 Feb 2006)
New Revision: 420
Modified:
trunk/ChangeLog
trunk/eval.c
Log:
2006-02-15(Wed) 14:21:45 +900 Yukihiro Matsumoto <matz ruby-lang.org>
* ChangeLog: add local variables line to support Emacs.
* eval.c (rb_obj_instance_exec): add new method from 1.9.
* eval.c (rb_mod_module_exec): ditto.
* eval.c (yield_under_i): should not pass self as an argument to
the block for instance_eval. [ruby-core:07364]
* eval.c (rb_obj_instance_eval): should be no singleton classes for
true, false, and nil. [ruby-dev:28186]
Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog 2006-02-14 10:36:56 UTC (rev 419)
+++ trunk/ChangeLog 2006-02-15 05:27:51 UTC (rev 420)
@@ -4,6 +4,20 @@
# from Mon, 03 May 2004 01:24:19 +0900
#
+2006-02-15(Wed) 14:21:45 +900 Yukihiro Matsumoto <matz ruby-lang.org>
+
+ * ChangeLog: add local variables line to support Emacs.
+
+ * eval.c (rb_obj_instance_exec): add new method from 1.9.
+
+ * eval.c (rb_mod_module_exec): ditto.
+
+ * eval.c (yield_under_i): should not pass self as an argument to
+ the block for instance_eval. [ruby-core:07364]
+
+ * eval.c (rb_obj_instance_eval): should be no singleton classes for
+ true, false, and nil. [ruby-dev:28186]
+
2006-02-14(Tue) 19:30:20 +0900 Koichi Sasada <ko1 atdot.net>
* array.c : fix indent
@@ -4809,4 +4823,13 @@
* 0.0.0.d : imported
-
+Local variables:
+add-log-time-format: (lambda ()
+ (let* ((time (current-time))
+ (diff (+ (cadr time) 32400))
+ (lo (% diff 65536))
+ (hi (+ (car time) (/ diff 65536))))
+ (format-time-string "%Y-%m-%d(%a) %H:%M:%S +900" (list hi lo) t)))
+indent-tabs-mode: t
+tab-width: 8
+end:
Modified: trunk/eval.c
===================================================================
--- trunk/eval.c 2006-02-14 10:36:56 UTC (rev 419)
+++ trunk/eval.c 2006-02-15 05:27:51 UTC (rev 420)
@@ -2132,16 +2132,21 @@
}
static VALUE
-yield_under_i(VALUE self)
+yield_under_i(VALUE arg)
{
- return rb_yield_0(self, 0, 0, 0, Qfalse);
+ int avalue = Qtrue;
+
+ if (arg == Qundef) {
+ avalue = Qfalse;
+ }
+ return rb_yield_0(arg, 0, 0, 0, avalue);
}
/* block eval under the class/module context */
static VALUE
-yield_under(VALUE under, VALUE self)
+yield_under(VALUE under, VALUE self, VALUE values)
{
- return exec_under(yield_under_i, under, self, self);
+ return exec_under(yield_under_i, under, self, values);
}
static VALUE
@@ -2178,7 +2183,7 @@
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)";
@@ -2237,7 +2242,7 @@
{
VALUE klass;
- if (FIXNUM_P(self) || SYMBOL_P(self)) {
+ if (SPECIAL_CONST_P(self)) {
klass = Qnil;
}
else {
@@ -2248,6 +2253,38 @@
/*
* 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 Klass
+ * def initialize
+ * @secret = 99
+ * end
+ * end
+ * k = Klass.new
+ * k.instance_exec(5) {|x| @secret+x } #=> 104
+ */
+
+VALUE
+rb_obj_instance_exec(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_values_new2(argc, argv));
+}
+
+/*
+ * call-seq:
* mod.class_eval(string [, filename [, lineno]]) => obj
* mod.module_eval {|| block } => obj
*
@@ -2276,6 +2313,32 @@
return specific_eval(argc, argv, mod, mod);
}
+/*
+ * call-seq:
+ * mod.module_exec(arg...) {|var...| block } => obj
+ * mod.class_exec(arg...) {|var...| block } => obj
+ *
+ * Evaluates the given block in the context of the class/module.
+ * The method defined in the block will belong to the receiver.
+ *
+ * class Thing
+ * end
+ * Thing.class_exec{
+ * def hello() "Hello there!" end
+ * }
+ * puts Thing.new.hello()
+ *
+ * <em>produces:</em>
+ *
+ * Hello there!
+ */
+
+VALUE
+rb_mod_module_exec(int argc, VALUE *argv, VALUE mod)
+{
+ return yield_under(mod, mod, rb_values_new2(argc, argv));
+}
+
static void
secure_visibility(VALUE self)
{
@@ -2843,6 +2906,7 @@
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);
@@ -2867,6 +2931,8 @@
rb_mod_private_method, -1);
rb_define_method(rb_cModule, "module_eval", rb_mod_module_eval, -1);
rb_define_method(rb_cModule, "class_eval", rb_mod_module_eval, -1);
+ rb_define_method(rb_cModule, "module_exec", rb_mod_module_exec, -1);
+ rb_define_method(rb_cModule, "class_exec", rb_mod_module_exec, -1);
rb_undef_method(rb_cClass, "module_function");
--
ML: yarv-diff quickml.atdot.net
Info: http://www.atdot.net/~ko1/quickml