ruby-changes:23076
From: shugo <ko1@a...>
Date: Sun, 25 Mar 2012 00:53:47 +0900 (JST)
Subject: [ruby-changes:23076] shugo:r35126 (trunk): * enumerator (lazy_initialize): set the instance variable "receiver"
shugo 2012-03-25 00:53:38 +0900 (Sun, 25 Mar 2012) New Revision: 35126 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35126 Log: * enumerator (lazy_initialize): set the instance variable "receiver" to include the receiver to the return value of inspect on a lazy enumerator directly created by Enumerator::Lazy.new. * enumerator (RETURN_LAZY): don't set the instance variable "receiver". Modified files: trunk/ChangeLog trunk/enumerator.c trunk/test/ruby/test_lazy_enumerator.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 35125) +++ ChangeLog (revision 35126) @@ -1,3 +1,11 @@ +Sun Mar 25 00:46:06 2012 Shugo Maeda <shugo@r...> + + * enumerator (lazy_initialize): set the instance variable "receiver" + to include the receiver to the return value of inspect on a lazy + enumerator directly created by Enumerator::Lazy.new. + + * enumerator (RETURN_LAZY): don't set the instance variable "receiver". + Sat Mar 24 23:59:00 2012 Shugo Maeda <shugo@r...> * enumerator (enumerator_inspect): include the original receiver and Index: enumerator.c =================================================================== --- enumerator.c (revision 35125) +++ enumerator.c (revision 35126) @@ -1244,31 +1244,17 @@ (rb_block_given_p() ? lazy_init_block_i : lazy_init_block), obj); enumerator_init(self, generator, meth, argc - offset, argv + offset); + rb_iv_set(self, "receiver", obj); return self; } -static void -lazy_set_inspection_data(VALUE obj, VALUE receiver, VALUE method) -{ - rb_iv_set(obj, "receiver", receiver); - if (NIL_P(method)) { - ID id = rb_frame_this_func(); - rb_iv_set(obj, "method", ID2SYM(id)); - } - else { - rb_iv_set(obj, "method", method); - } -} - -/* - * An ugly macro to set inspection data to arg, and return arg. - * RETURN_LAZY assumes that the reciver is obj. - */ -#define RETURN_LAZY(arg) do { \ - VALUE lazy = arg; \ - lazy_set_inspection_data(lazy, obj, Qnil); \ - return lazy; \ +/* A macro to set the current method name to lazy and return lazy. */ +#define RETURN_LAZY(lazy) do { \ + VALUE result = lazy; \ + ID id = rb_frame_this_func(); \ + rb_iv_set(result, "method", ID2SYM(id)); \ + return result; \ } while (0) /* @@ -1309,7 +1295,7 @@ result = rb_class_new_instance(1, &obj, rb_cLazy); /* Qfalse indicates that the Enumerator::Lazy has no method name */ - lazy_set_inspection_data(result, obj, Qfalse); + rb_iv_set(result, "method", Qfalse); return result; } Index: test/ruby/test_lazy_enumerator.rb =================================================================== --- test/ruby/test_lazy_enumerator.rb (revision 35125) +++ test/ruby/test_lazy_enumerator.rb (revision 35126) @@ -282,6 +282,10 @@ end def test_inspect + assert_equal("#<Enumerator::Lazy: 1..10:each>", + Enumerator::Lazy.new(1..10).inspect) + assert_equal("#<Enumerator::Lazy: 1..10:cycle(2)>", + Enumerator::Lazy.new(1..10, :cycle, 2).inspect) assert_equal("#<Enumerator::Lazy: 1..10>", (1..10).lazy.inspect) assert_equal('#<Enumerator::Lazy: #<Enumerator: "foo":chars>>', "foo".chars.lazy.inspect) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/