ruby-changes:25441
From: marcandre <ko1@a...>
Date: Wed, 7 Nov 2012 02:10:31 +0900 (JST)
Subject: [ruby-changes:25441] marcandRe: r37498 (trunk): * enumerator.c (obj_to_enum): Have #to_enum accept a block
marcandre 2012-11-07 02:10:20 +0900 (Wed, 07 Nov 2012) New Revision: 37498 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37498 Log: * enumerator.c (obj_to_enum): Have #to_enum accept a block [Feature #6636] Modified files: trunk/enumerator.c trunk/test/ruby/test_enumerator.rb Index: enumerator.c =================================================================== --- enumerator.c (revision 37497) +++ enumerator.c (revision 37498) @@ -186,6 +186,8 @@ * call-seq: * obj.to_enum(method = :each, *args) * obj.enum_for(method = :each, *args) + * obj.to_enum(method = :each, *args) {|obj, *args| block} + * obj.enum_for(method = :each, *args){|obj, *args| block} * * Creates a new Enumerator which will enumerate by on calling +method+ on * +obj+. @@ -195,6 +197,9 @@ * to the item itself. Note that the number of args * must not exceed the number expected by +method+ * + * If a block is given, it will be used to calculate the size of + * the enumerator (see Enumerator#size=). + * * === Example * * str = "xyz" @@ -213,13 +218,17 @@ static VALUE obj_to_enum(int argc, VALUE *argv, VALUE obj) { - VALUE meth = sym_each; + VALUE enumerator, meth = sym_each; if (argc > 0) { --argc; meth = *argv++; } - return rb_enumeratorize(obj, meth, argc, argv, 0); + enumerator = rb_enumeratorize(obj, meth, argc, argv, 0); + if (rb_block_given_p()) { + enumerator_ptr(enumerator)->size = rb_block_proc(); + } + return enumerator; } static VALUE Index: test/ruby/test_enumerator.rb =================================================================== --- test/ruby/test_enumerator.rb (revision 37497) +++ test/ruby/test_enumerator.rb (revision 37498) @@ -410,6 +410,9 @@ assert_equal Float::INFINITY, Enumerator.new(Float::INFINITY){}.size assert_equal nil, Enumerator.new(nil){}.size assert_raise(TypeError) { Enumerator.new("42"){} } + + assert_equal nil, @obj.to_enum(:foo, 0, 1).size + assert_equal 2, @obj.to_enum(:foo, 0, 1){ 2 }.size end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/