mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* array.c (take_items), enum.c (enum_zip): tries to convert to
array first. [ruby-core:21442] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21678 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
13313688b2
commit
cab67c3197
5 changed files with 24 additions and 3 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Tue Jan 20 06:48:56 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* array.c (take_items), enum.c (enum_zip): tries to convert to
|
||||||
|
array first. [ruby-core:21442]
|
||||||
|
|
||||||
Tue Jan 20 03:50:37 2009 NARUSE, Yui <naruse@ruby-lang.org>
|
Tue Jan 20 03:50:37 2009 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* ext/nkf/nkf-utf8/nkf.c: Update nkf to 2.0.9.
|
* ext/nkf/nkf-utf8/nkf.c: Update nkf to 2.0.9.
|
||||||
|
|
4
array.c
4
array.c
|
@ -2209,9 +2209,11 @@ take_i(VALUE val, VALUE *args, int argc, VALUE *argv)
|
||||||
static VALUE
|
static VALUE
|
||||||
take_items(VALUE obj, long n)
|
take_items(VALUE obj, long n)
|
||||||
{
|
{
|
||||||
VALUE result = rb_ary_new2(n);
|
VALUE result = to_ary(obj);
|
||||||
VALUE args[2];
|
VALUE args[2];
|
||||||
|
|
||||||
|
if (!NIL_P(result)) return rb_ary_subseq(result, 0, n);
|
||||||
|
result = rb_ary_new2(n);
|
||||||
args[0] = result; args[1] = (VALUE)n;
|
args[0] = result; args[1] = (VALUE)n;
|
||||||
rb_block_call(obj, rb_intern("each"), 0, 0, take_i, (VALUE)args);
|
rb_block_call(obj, rb_intern("each"), 0, 0, take_i, (VALUE)args);
|
||||||
return result;
|
return result;
|
||||||
|
|
8
enum.c
8
enum.c
|
@ -1558,13 +1558,17 @@ enum_zip(int argc, VALUE *argv, VALUE obj)
|
||||||
ID conv;
|
ID conv;
|
||||||
NODE *memo;
|
NODE *memo;
|
||||||
VALUE result = Qnil;
|
VALUE result = Qnil;
|
||||||
|
VALUE args = rb_ary_new4(argc, argv);
|
||||||
int allary = Qtrue;
|
int allary = Qtrue;
|
||||||
|
|
||||||
|
argv = RARRAY_PTR(args);
|
||||||
for (i=0; i<argc; i++) {
|
for (i=0; i<argc; i++) {
|
||||||
if (TYPE(argv[i]) != T_ARRAY) {
|
VALUE ary = rb_check_array_type(argv[i]);
|
||||||
|
if (NIL_P(ary)) {
|
||||||
allary = Qfalse;
|
allary = Qfalse;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
argv[i] = ary;
|
||||||
}
|
}
|
||||||
if (!allary) {
|
if (!allary) {
|
||||||
CONST_ID(conv, "to_enum");
|
CONST_ID(conv, "to_enum");
|
||||||
|
@ -1576,7 +1580,7 @@ enum_zip(int argc, VALUE *argv, VALUE obj)
|
||||||
result = rb_ary_new();
|
result = rb_ary_new();
|
||||||
}
|
}
|
||||||
/* use NODE_DOT2 as memo(v, v, -) */
|
/* use NODE_DOT2 as memo(v, v, -) */
|
||||||
memo = rb_node_newnode(NODE_DOT2, result, rb_ary_new4(argc, argv), 0);
|
memo = rb_node_newnode(NODE_DOT2, result, args, 0);
|
||||||
rb_block_call(obj, id_each, 0, 0, allary ? zip_ary : zip_i, (VALUE)memo);
|
rb_block_call(obj, id_each, 0, 0, allary ? zip_ary : zip_i, (VALUE)memo);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -1502,6 +1502,11 @@ class TestArray < Test::Unit::TestCase
|
||||||
a = []
|
a = []
|
||||||
[1, 2, 3].zip([:a, :b], ["a", "b", "c", "d"]) {|x| a << x }
|
[1, 2, 3].zip([:a, :b], ["a", "b", "c", "d"]) {|x| a << x }
|
||||||
assert_equal([[1, :a, "a"], [2, :b, "b"], [3, nil, "c"]], a)
|
assert_equal([[1, :a, "a"], [2, :b, "b"], [3, nil, "c"]], a)
|
||||||
|
|
||||||
|
ary = Object.new
|
||||||
|
def ary.to_a; [1, 2]; end
|
||||||
|
def ary.to_ary; [3, 4]; end
|
||||||
|
assert_equal([[5, 3], [6, 4]], [5, 6].zip(ary))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_transpose
|
def test_transpose
|
||||||
|
|
|
@ -211,6 +211,11 @@ class TestEnumerable < Test::Unit::TestCase
|
||||||
a = []
|
a = []
|
||||||
@obj.zip([:a, :b, :c]) {|x,y| a << [x, y] }
|
@obj.zip([:a, :b, :c]) {|x,y| a << [x, y] }
|
||||||
assert_equal([[1,:a],[2,:b],[3,:c],[1,nil],[2,nil]], a)
|
assert_equal([[1,:a],[2,:b],[3,:c],[1,nil],[2,nil]], a)
|
||||||
|
|
||||||
|
ary = Object.new
|
||||||
|
def ary.to_a; [1, 2]; end
|
||||||
|
def ary.to_ary; [3, 4]; end
|
||||||
|
assert_equal([[1, 3], [2, 4], [3, nil], [1, nil], [2, nil]], @obj.zip(ary))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_take
|
def test_take
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue