зеркало из https://github.com/github/ruby.git
* enumerator.c: Support #size for enumerators created from enumerators
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37499 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
acfd34a6a9
Коммит
57d596cdb4
|
@ -489,6 +489,9 @@ enumerator_with_index_i(VALUE val, VALUE m, int argc, VALUE *argv)
|
|||
return rb_yield_values(2, rb_ary_new4(argc, argv), idx);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
enumerator_size(VALUE obj);
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* e.with_index(offset = 0) {|(*args), idx| ... }
|
||||
|
@ -507,7 +510,7 @@ enumerator_with_index(int argc, VALUE *argv, VALUE obj)
|
|||
VALUE memo;
|
||||
|
||||
rb_scan_args(argc, argv, "01", &memo);
|
||||
RETURN_ENUMERATOR(obj, argc, argv);
|
||||
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_size);
|
||||
memo = NIL_P(memo) ? 0 : (VALUE)NUM2LONG(memo);
|
||||
return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)&memo);
|
||||
}
|
||||
|
@ -567,7 +570,7 @@ enumerator_with_object_i(VALUE val, VALUE memo, int argc, VALUE *argv)
|
|||
static VALUE
|
||||
enumerator_with_object(VALUE obj, VALUE memo)
|
||||
{
|
||||
RETURN_ENUMERATOR(obj, 1, &memo);
|
||||
RETURN_SIZED_ENUMERATOR(obj, 1, &memo, enumerator_size);
|
||||
enumerator_block_call(obj, enumerator_with_object_i, memo);
|
||||
|
||||
return memo;
|
||||
|
|
|
@ -414,5 +414,11 @@ class TestEnumerator < Test::Unit::TestCase
|
|||
assert_equal nil, @obj.to_enum(:foo, 0, 1).size
|
||||
assert_equal 2, @obj.to_enum(:foo, 0, 1){ 2 }.size
|
||||
end
|
||||
|
||||
def test_size_for_enum_created_by_enumerators
|
||||
enum = to_enum{ 42 }
|
||||
assert_equal 42, enum.with_index.size
|
||||
assert_equal 42, enum.with_object(:foo).size
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче