[前][次][番号順一覧][スレッド一覧]

ruby-changes:10271

From: yugui <ko1@a...>
Date: Wed, 28 Jan 2009 18:21:15 +0900 (JST)
Subject: [ruby-changes:10271] Ruby:r21814 (ruby_1_9_1): introduces r21693 again and merges r21713

yugui	2009-01-28 18:20:03 +0900 (Wed, 28 Jan 2009)

  New Revision: 21814

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=21814

  Log:
    introduces r21693 again and merges r21713
    * array.c (take_items), enum.c (enum_zip): tries to convert to
      array first.  [ruby-core:21442]
    --
    * array.c (take_items): to_ary() raises ArgumentError if cannot to
      convert to Array. [ruby-dev:37797]

  Modified files:
    branches/ruby_1_9_1/ChangeLog
    branches/ruby_1_9_1/array.c
    branches/ruby_1_9_1/enum.c
    branches/ruby_1_9_1/test/ruby/test_array.rb
    branches/ruby_1_9_1/test/ruby/test_enum.rb

Index: ruby_1_9_1/array.c
===================================================================
--- ruby_1_9_1/array.c	(revision 21813)
+++ ruby_1_9_1/array.c	(revision 21814)
@@ -2209,9 +2209,11 @@
 static VALUE
 take_items(VALUE obj, long n)
 {
-    VALUE result = rb_ary_new2(n);
+    VALUE result = rb_check_array_type(obj);
     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;
     rb_block_call(obj, rb_intern("each"), 0, 0, take_i, (VALUE)args);
     return result;
Index: ruby_1_9_1/ChangeLog
===================================================================
--- ruby_1_9_1/ChangeLog	(revision 21813)
+++ ruby_1_9_1/ChangeLog	(revision 21814)
@@ -1,3 +1,12 @@
+Wed Jan 21 14:41:48 2009  NAKAMURA Usaku  <usa@r...>
+
+	* array.c (take_items): to_ary() raises ArgumentError if cannot to
+	  convert to Array. [ruby-dev:37797]
+
+Wed Jan 21 21:12:47 2009  Yuki Sonoda (Yugui)  <yugui@y...>
+
+	* introduces r21693 again for merging r21713.
+
 Tue Jan 20 19:12:18 2009  Yuki Sonoda (Yugui)  <yugui@y...>
 
 	* reverts r21693.
Index: ruby_1_9_1/enum.c
===================================================================
--- ruby_1_9_1/enum.c	(revision 21813)
+++ ruby_1_9_1/enum.c	(revision 21814)
@@ -1558,13 +1558,17 @@
     ID conv;
     NODE *memo;
     VALUE result = Qnil;
+    VALUE args = rb_ary_new4(argc, argv);
     int allary = Qtrue;
 
+    argv = RARRAY_PTR(args);
     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;
 	    break;
 	}
+	argv[i] = ary;
     }
     if (!allary) {
 	CONST_ID(conv, "to_enum");
@@ -1576,7 +1580,7 @@
 	result = rb_ary_new();
     }
     /* 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);
 
     return result;
Index: ruby_1_9_1/test/ruby/test_array.rb
===================================================================
--- ruby_1_9_1/test/ruby/test_array.rb	(revision 21813)
+++ ruby_1_9_1/test/ruby/test_array.rb	(revision 21814)
@@ -1502,6 +1502,14 @@
     a = []
     [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)
+
+    ary = Object.new
+    def ary.to_a;   [1, 2]; end
+    assert_raise(NoMethodError){ %w(a b).zip(ary) }
+    def ary.each; [3, 4].each{|e|yield e}; end
+    assert_equal([['a', 3], ['b', 4]], %w(a b).zip(ary))
+    def ary.to_ary; [5, 6]; end
+    assert_equal([['a', 5], ['b', 6]], %w(a b).zip(ary))
   end
 
   def test_transpose
Index: ruby_1_9_1/test/ruby/test_enum.rb
===================================================================
--- ruby_1_9_1/test/ruby/test_enum.rb	(revision 21813)
+++ ruby_1_9_1/test/ruby/test_enum.rb	(revision 21814)
@@ -211,6 +211,14 @@
     a = []
     @obj.zip([:a, :b, :c]) {|x,y| a << [x, y] }
     assert_equal([[1,:a],[2,:b],[3,:c],[1,nil],[2,nil]], a)
+
+    ary = Object.new
+    def ary.to_a;   [1, 2]; end
+    assert_raise(NoMethodError){ %w(a b).zip(ary) }
+    def ary.each; [3, 4].each{|e|yield e}; end
+    assert_equal([[1, 3], [2, 4], [3, nil], [1, nil], [2, nil]], @obj.zip(ary))
+    def ary.to_ary; [5, 6]; end
+    assert_equal([[1, 5], [2, 6], [3, nil], [1, nil], [2, nil]], @obj.zip(ary))
   end
 
   def test_take

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]