ruby-changes:25466
From: marcandre <ko1@a...>
Date: Wed, 7 Nov 2012 02:19:20 +0900 (JST)
Subject: [ruby-changes:25466] marcandRe: r37523 (trunk): * enumerator.c: Support for lazy.take.size
marcandre 2012-11-07 02:16:29 +0900 (Wed, 07 Nov 2012) New Revision: 37523 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37523 Log: * enumerator.c: Support for lazy.take.size [Feature #6636] Modified files: trunk/enumerator.c trunk/test/ruby/test_lazy_enumerator.rb Index: enumerator.c =================================================================== --- enumerator.c (revision 37522) +++ enumerator.c (revision 37523) @@ -1625,6 +1625,15 @@ } static VALUE +lazy_take_size(VALUE lazy) { + long len = NUM2LONG(RARRAY_PTR(rb_ivar_get(lazy, id_arguments))[0]); + VALUE receiver = lazy_receiver_size(lazy); + if (NIL_P(receiver) || (FIXNUM_P(receiver) && FIX2LONG(receiver) < len)) + return receiver; + return LONG2NUM(len); +} + +static VALUE lazy_take(VALUE obj, VALUE n) { NODE *memo; @@ -1644,7 +1653,7 @@ memo = NEW_MEMO(0, len, len); return lazy_set_method(rb_block_call(rb_cLazy, id_new, argc, argv, lazy_take_func, (VALUE) memo), - rb_ary_new3(1, n), 0); + rb_ary_new3(1, n), lazy_take_size); } static VALUE Index: test/ruby/test_lazy_enumerator.rb =================================================================== --- test/ruby/test_lazy_enumerator.rb (revision 37522) +++ test/ruby/test_lazy_enumerator.rb (revision 37523) @@ -336,5 +336,10 @@ assert_equal nil, lazy.send(m){}.size end assert_equal nil, lazy.grep(//).size + + assert_equal 2, lazy.take(2).size + assert_equal 3, lazy.take(4).size + assert_equal 4, loop.lazy.take(4).size + assert_equal nil, lazy.select{}.take(4).size end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/