array.c (rb_ary_zip): take only as many as needed from an Enumerator (#4389)
[Bug #17814]
This commit is contained in:
parent
d427e3cd6f
commit
fb04c69418
Notes:
git
2021-04-21 13:02:56 +09:00
Merged-By: mame <mame@ruby-lang.org>
4
array.c
4
array.c
@ -4311,10 +4311,9 @@ static VALUE
|
|||||||
take_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, cbarg))
|
take_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, cbarg))
|
||||||
{
|
{
|
||||||
VALUE *args = (VALUE *)cbarg;
|
VALUE *args = (VALUE *)cbarg;
|
||||||
if (args[1] == 0) rb_iter_break();
|
|
||||||
else args[1]--;
|
|
||||||
if (argc > 1) val = rb_ary_new4(argc, argv);
|
if (argc > 1) val = rb_ary_new4(argc, argv);
|
||||||
rb_ary_push(args[0], val);
|
rb_ary_push(args[0], val);
|
||||||
|
if (--args[1] == 0) rb_iter_break();
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4324,6 +4323,7 @@ take_items(VALUE obj, long n)
|
|||||||
VALUE result = rb_check_array_type(obj);
|
VALUE result = rb_check_array_type(obj);
|
||||||
VALUE args[2];
|
VALUE args[2];
|
||||||
|
|
||||||
|
if (n == 0) return result;
|
||||||
if (!NIL_P(result)) return rb_ary_subseq(result, 0, n);
|
if (!NIL_P(result)) return rb_ary_subseq(result, 0, n);
|
||||||
result = rb_ary_new2(n);
|
result = rb_ary_new2(n);
|
||||||
args[0] = result; args[1] = (VALUE)n;
|
args[0] = result; args[1] = (VALUE)n;
|
||||||
|
@ -2735,6 +2735,17 @@ class TestArray < Test::Unit::TestCase
|
|||||||
assert_equal [[42, 1]], [42].zip(r), bug8153
|
assert_equal [[42, 1]], [42].zip(r), bug8153
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_zip_with_enumerator
|
||||||
|
bug17814 = "ruby-core:103513"
|
||||||
|
|
||||||
|
step = 0.step
|
||||||
|
e = Enumerator.produce { step.next }
|
||||||
|
a = %w(a b c)
|
||||||
|
assert_equal([["a", 0], ["b", 1], ["c", 2]], a.zip(e))
|
||||||
|
assert_equal([["a", 3], ["b", 4], ["c", 5]], a.zip(e))
|
||||||
|
assert_equal([["a", 6], ["b", 7], ["c", 8]], a.zip(e))
|
||||||
|
end
|
||||||
|
|
||||||
def test_transpose
|
def test_transpose
|
||||||
assert_equal([[1, :a], [2, :b], [3, :c]],
|
assert_equal([[1, :a], [2, :b], [3, :c]],
|
||||||
[[1, 2, 3], [:a, :b, :c]].transpose)
|
[[1, 2, 3], [:a, :b, :c]].transpose)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user