mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enumerator.c (lazy_take): don't enumerate an extra value.
[ruby-dev:45370] [Bug #6152] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35050 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a1f4163dd3
commit
29f73009ca
3 changed files with 25 additions and 5 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Fri Mar 16 11:20:07 2012 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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@ruby-lang.org>
|
Fri Mar 16 06:30:10 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* enumerator.c (lazy_zip_func): variadic argument needs explicit cast
|
* enumerator.c (lazy_zip_func): variadic argument needs explicit cast
|
||||||
|
|
19
enumerator.c
19
enumerator.c
|
@ -1449,10 +1449,13 @@ lazy_take_func(VALUE val, VALUE args, int argc, VALUE *argv)
|
||||||
{
|
{
|
||||||
NODE *memo = RNODE(args);
|
NODE *memo = RNODE(args);
|
||||||
|
|
||||||
if (memo->u3.cnt == 0) return Qundef;
|
|
||||||
rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
|
rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
|
||||||
memo->u3.cnt--;
|
if (--memo->u3.cnt == 0) {
|
||||||
return Qnil;
|
return Qundef;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
@ -1460,12 +1463,20 @@ lazy_take(VALUE obj, VALUE n)
|
||||||
{
|
{
|
||||||
NODE *memo;
|
NODE *memo;
|
||||||
long len = NUM2LONG(n);
|
long len = NUM2LONG(n);
|
||||||
|
int argc = 1;
|
||||||
|
VALUE argv[3];
|
||||||
|
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
rb_raise(rb_eArgError, "attempt to take negative size");
|
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);
|
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);
|
(VALUE) memo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -201,7 +201,11 @@ class TestLazyEnumerator < Test::Unit::TestCase
|
||||||
assert_equal(5, a.current)
|
assert_equal(5, a.current)
|
||||||
assert_equal(1, a.lazy.take(5).first)
|
assert_equal(1, a.lazy.take(5).first)
|
||||||
assert_equal(1, a.current)
|
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
|
end
|
||||||
|
|
||||||
def test_take_while
|
def test_take_while
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue