mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enumerator.c: Allow Enumerator size argument to be any callable.
Patch by Avdi Grimm. [bug #8641] [ruby-core:56032] [fix GH-362] * test/ruby/test_enumerator.rb: Test for above git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42698 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c4fe526011
commit
ca7f52a32a
2 changed files with 12 additions and 6 deletions
15
enumerator.c
15
enumerator.c
|
@ -339,7 +339,7 @@ enumerator_initialize(int argc, VALUE *argv, VALUE obj)
|
|||
rb_check_arity(argc, 0, 1);
|
||||
recv = generator_init(generator_allocate(rb_cGenerator), rb_block_proc());
|
||||
if (argc) {
|
||||
if (NIL_P(argv[0]) || rb_obj_is_proc(argv[0]) ||
|
||||
if (NIL_P(argv[0]) || rb_respond_to(argv[0], id_call) ||
|
||||
(RB_TYPE_P(argv[0], T_FLOAT) && RFLOAT_VALUE(argv[0]) == INFINITY)) {
|
||||
size = argv[0];
|
||||
}
|
||||
|
@ -1007,11 +1007,14 @@ enumerator_size(VALUE obj)
|
|||
if (e->size_fn) {
|
||||
return (*e->size_fn)(e->obj, e->args, obj);
|
||||
}
|
||||
if (rb_obj_is_proc(e->size)) {
|
||||
if (e->args)
|
||||
return rb_proc_call(e->size, e->args);
|
||||
else
|
||||
return rb_proc_call_with_block(e->size, 0, 0, Qnil);
|
||||
if (rb_respond_to(e->size, id_call)) {
|
||||
if (e->args) {
|
||||
int argc = RARRAY_LENINT(e->args);
|
||||
VALUE *argv = RARRAY_PTR(e->args);
|
||||
return rb_funcall2(e->size, id_call, argc, argv);
|
||||
} else {
|
||||
return rb_funcall(e->size, id_call, 0);
|
||||
}
|
||||
}
|
||||
return e->size;
|
||||
}
|
||||
|
|
|
@ -452,6 +452,9 @@ class TestEnumerator < Test::Unit::TestCase
|
|||
def test_size
|
||||
assert_equal nil, Enumerator.new{}.size
|
||||
assert_equal 42, Enumerator.new(->{42}){}.size
|
||||
obj = Object.new
|
||||
def obj.call; 42; end
|
||||
assert_equal 42, Enumerator.new(obj){}.size
|
||||
assert_equal 42, Enumerator.new(42){}.size
|
||||
assert_equal 1 << 70, Enumerator.new(1 << 70){}.size
|
||||
assert_equal Float::INFINITY, Enumerator.new(Float::INFINITY){}.size
|
||||
|
|
Loading…
Reference in a new issue