ruby-changes:23001
From: shugo <ko1@a...>
Date: Fri, 16 Mar 2012 11:22:33 +0900 (JST)
Subject: [ruby-changes:23001] shugo:r35050 (trunk): * enumerator.c (lazy_take): don't enumerate an extra value.
shugo 2012-03-16 11:22:20 +0900 (Fri, 16 Mar 2012) New Revision: 35050 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35050 Log: * enumerator.c (lazy_take): don't enumerate an extra value. [ruby-dev:45370] [Bug #6152] Modified files: trunk/ChangeLog trunk/enumerator.c trunk/test/ruby/test_lazy_enumerator.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 35049) +++ ChangeLog (revision 35050) @@ -1,3 +1,8 @@ +Fri Mar 16 11:20:07 2012 Shugo Maeda <shugo@r...> + + * enumerator.c (lazy_take): don't enumerate an extra value. + [ruby-dev:45370] [Bug #6152] + Fri Mar 16 06:30:10 2012 Nobuyoshi Nakada <nobu@r...> * enumerator.c (lazy_zip_func): variadic argument needs explicit cast Index: enumerator.c =================================================================== --- enumerator.c (revision 35049) +++ enumerator.c (revision 35050) @@ -1449,10 +1449,13 @@ { NODE *memo = RNODE(args); - if (memo->u3.cnt == 0) return Qundef; rb_funcall2(argv[0], id_yield, argc - 1, argv + 1); - memo->u3.cnt--; - return Qnil; + if (--memo->u3.cnt == 0) { + return Qundef; + } + else { + return Qnil; + } } static VALUE @@ -1460,12 +1463,20 @@ { NODE *memo; long len = NUM2LONG(n); + int argc = 1; + VALUE argv[3]; if (len < 0) { rb_raise(rb_eArgError, "attempt to take negative size"); } + argv[0] = obj; + if (len == 0) { + argv[1] = sym_cycle; + argv[2] = INT2NUM(0); + argc = 3; + } memo = NEW_MEMO(0, 0, len); - return rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_take_func, + return rb_block_call(rb_cLazy, id_new, argc, argv, lazy_take_func, (VALUE) memo); } Index: test/ruby/test_lazy_enumerator.rb =================================================================== --- test/ruby/test_lazy_enumerator.rb (revision 35049) +++ test/ruby/test_lazy_enumerator.rb (revision 35050) @@ -201,7 +201,11 @@ assert_equal(5, a.current) assert_equal(1, a.lazy.take(5).first) assert_equal(1, a.current) - assert_equal((1..5).to_a, a.lazy.take(5).to_a) + assert_equal((1..5).to_a, a.lazy.take(5).force) + assert_equal(5, a.current) + a = Step.new(1..10) + assert_equal([], a.lazy.take(0).force) + assert_equal(nil, a.current) end def test_take_while -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/