* enumerator.c (obj_to_enum): Have #to_enum accept a block
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37498 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
610eeffa46
commit
acfd34a6a9
13
enumerator.c
13
enumerator.c
@ -186,6 +186,8 @@ enumerator_ptr(VALUE obj)
|
|||||||
* call-seq:
|
* call-seq:
|
||||||
* obj.to_enum(method = :each, *args)
|
* obj.to_enum(method = :each, *args)
|
||||||
* obj.enum_for(method = :each, *args)
|
* obj.enum_for(method = :each, *args)
|
||||||
|
* obj.to_enum(method = :each, *args) {|obj, *args| block}
|
||||||
|
* obj.enum_for(method = :each, *args){|obj, *args| block}
|
||||||
*
|
*
|
||||||
* Creates a new Enumerator which will enumerate by on calling +method+ on
|
* Creates a new Enumerator which will enumerate by on calling +method+ on
|
||||||
* +obj+.
|
* +obj+.
|
||||||
@ -195,6 +197,9 @@ enumerator_ptr(VALUE obj)
|
|||||||
* to the item itself. Note that the number of args
|
* to the item itself. Note that the number of args
|
||||||
* must not exceed the number expected by +method+
|
* must not exceed the number expected by +method+
|
||||||
*
|
*
|
||||||
|
* If a block is given, it will be used to calculate the size of
|
||||||
|
* the enumerator (see Enumerator#size=).
|
||||||
|
*
|
||||||
* === Example
|
* === Example
|
||||||
*
|
*
|
||||||
* str = "xyz"
|
* str = "xyz"
|
||||||
@ -213,13 +218,17 @@ enumerator_ptr(VALUE obj)
|
|||||||
static VALUE
|
static VALUE
|
||||||
obj_to_enum(int argc, VALUE *argv, VALUE obj)
|
obj_to_enum(int argc, VALUE *argv, VALUE obj)
|
||||||
{
|
{
|
||||||
VALUE meth = sym_each;
|
VALUE enumerator, meth = sym_each;
|
||||||
|
|
||||||
if (argc > 0) {
|
if (argc > 0) {
|
||||||
--argc;
|
--argc;
|
||||||
meth = *argv++;
|
meth = *argv++;
|
||||||
}
|
}
|
||||||
return rb_enumeratorize(obj, meth, argc, argv, 0);
|
enumerator = rb_enumeratorize(obj, meth, argc, argv, 0);
|
||||||
|
if (rb_block_given_p()) {
|
||||||
|
enumerator_ptr(enumerator)->size = rb_block_proc();
|
||||||
|
}
|
||||||
|
return enumerator;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
@ -410,6 +410,9 @@ class TestEnumerator < Test::Unit::TestCase
|
|||||||
assert_equal Float::INFINITY, Enumerator.new(Float::INFINITY){}.size
|
assert_equal Float::INFINITY, Enumerator.new(Float::INFINITY){}.size
|
||||||
assert_equal nil, Enumerator.new(nil){}.size
|
assert_equal nil, Enumerator.new(nil){}.size
|
||||||
assert_raise(TypeError) { Enumerator.new("42"){} }
|
assert_raise(TypeError) { Enumerator.new("42"){} }
|
||||||
|
|
||||||
|
assert_equal nil, @obj.to_enum(:foo, 0, 1).size
|
||||||
|
assert_equal 2, @obj.to_enum(:foo, 0, 1){ 2 }.size
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user