[前][次][番号順一覧][スレッド一覧]

ruby-changes:30619

From: marcandre <ko1@a...>
Date: Tue, 27 Aug 2013 11:50:13 +0900 (JST)
Subject: [ruby-changes:30619] marcandRe: r42698 (trunk): * enumerator.c: Allow Enumerator size argument to be any callable.

marcandre	2013-08-27 11:50:08 +0900 (Tue, 27 Aug 2013)

  New Revision: 42698

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42698

  Log:
    * 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

  Modified files:
    trunk/enumerator.c
    trunk/test/ruby/test_enumerator.rb
Index: enumerator.c
===================================================================
--- enumerator.c	(revision 42697)
+++ enumerator.c	(revision 42698)
@@ -339,7 +339,7 @@ enumerator_initialize(int argc, VALUE *a https://github.com/ruby/ruby/blob/trunk/enumerator.c#L339
 	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) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L1007
     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;
 }
Index: test/ruby/test_enumerator.rb
===================================================================
--- test/ruby/test_enumerator.rb	(revision 42697)
+++ test/ruby/test_enumerator.rb	(revision 42698)
@@ -452,6 +452,9 @@ class TestEnumerator < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enumerator.rb#L452
   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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]