Skip to content

Commit

Permalink
* enumerator.c: Support #size for enumerators created from enumerators
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Oct 31, 2012
1 parent 82cc755 commit 6fca009
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions enumerator.c
Expand Up @@ -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| ... }
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions test/ruby/test_enumerator.rb
Expand Up @@ -414,5 +414,11 @@ def test_size
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

0 comments on commit 6fca009

Please sign in to comment.