From 0c9f66eb400a78ef2e201a30a3904f59033d0439 Mon Sep 17 00:00:00 2001 From: shugo Date: Sat, 24 Mar 2012 15:53:38 +0000 Subject: * 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". git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ enumerator.c | 30 ++++++++---------------------- test/ruby/test_lazy_enumerator.rb | 4 ++++ 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index b3716edc2c..5c101f95c5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Sun Mar 25 00:46:06 2012 Shugo Maeda + + * 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 * enumerator (enumerator_inspect): include the original receiver and diff --git a/enumerator.c b/enumerator.c index c7d3d799e4..cc19cacfed 100644 --- a/enumerator.c +++ b/enumerator.c @@ -1244,31 +1244,17 @@ lazy_initialize(int argc, VALUE *argv, VALUE self) (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 @@ enumerable_lazy(VALUE obj) 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; } diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb index 6367d918a5..d51b33bde4 100644 --- a/test/ruby/test_lazy_enumerator.rb +++ b/test/ruby/test_lazy_enumerator.rb @@ -282,6 +282,10 @@ class TestLazyEnumerator < Test::Unit::TestCase end def test_inspect + assert_equal("#", + Enumerator::Lazy.new(1..10).inspect) + assert_equal("#", + Enumerator::Lazy.new(1..10, :cycle, 2).inspect) assert_equal("#", (1..10).lazy.inspect) assert_equal('#>', "foo".chars.lazy.inspect) -- cgit v1.2.3