зеркало из https://github.com/github/ruby.git
* 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
This commit is contained in:
Родитель
fa288063e0
Коммит
0c9f66eb40
|
@ -1,3 +1,11 @@
|
|||
Sun Mar 25 00:46:06 2012 Shugo Maeda <shugo@ruby-lang.org>
|
||||
|
||||
* 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@ruby-lang.org>
|
||||
|
||||
* enumerator (enumerator_inspect): include the original receiver and
|
||||
|
|
30
enumerator.c
30
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -282,6 +282,10 @@ class TestLazyEnumerator < Test::Unit::TestCase
|
|||
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)
|
||||
|
|
Загрузка…
Ссылка в новой задаче