Skip to content

Commit 6fca009

Browse files
committed
* enumerator.c: Support #size for enumerators created from enumerators
1 parent 82cc755 commit 6fca009

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

enumerator.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ enumerator_with_index_i(VALUE val, VALUE m, int argc, VALUE *argv)
489489
return rb_yield_values(2, rb_ary_new4(argc, argv), idx);
490490
}
491491

492+
static VALUE
493+
enumerator_size(VALUE obj);
494+
492495
/*
493496
* call-seq:
494497
* e.with_index(offset = 0) {|(*args), idx| ... }
@@ -507,7 +510,7 @@ enumerator_with_index(int argc, VALUE *argv, VALUE obj)
507510
VALUE memo;
508511

509512
rb_scan_args(argc, argv, "01", &memo);
510-
RETURN_ENUMERATOR(obj, argc, argv);
513+
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_size);
511514
memo = NIL_P(memo) ? 0 : (VALUE)NUM2LONG(memo);
512515
return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)&memo);
513516
}
@@ -567,7 +570,7 @@ enumerator_with_object_i(VALUE val, VALUE memo, int argc, VALUE *argv)
567570
static VALUE
568571
enumerator_with_object(VALUE obj, VALUE memo)
569572
{
570-
RETURN_ENUMERATOR(obj, 1, &memo);
573+
RETURN_SIZED_ENUMERATOR(obj, 1, &memo, enumerator_size);
571574
enumerator_block_call(obj, enumerator_with_object_i, memo);
572575

573576
return memo;

test/ruby/test_enumerator.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,5 +414,11 @@ def test_size
414414
assert_equal nil, @obj.to_enum(:foo, 0, 1).size
415415
assert_equal 2, @obj.to_enum(:foo, 0, 1){ 2 }.size
416416
end
417+
418+
def test_size_for_enum_created_by_enumerators
419+
enum = to_enum{ 42 }
420+
assert_equal 42, enum.with_index.size
421+
assert_equal 42, enum.with_object(:foo).size
422+
end
417423
end
418424

0 commit comments

Comments
 (0)