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

ruby-changes:32980

From: glass <ko1@a...>
Date: Thu, 20 Feb 2014 12:09:18 +0900 (JST)
Subject: [ruby-changes:32980] glass:r45059 (trunk): * hash.c (rb_hash_flatten): fix behavior of flatten(0).

glass	2014-02-20 12:09:13 +0900 (Thu, 20 Feb 2014)

  New Revision: 45059

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

  Log:
    * hash.c (rb_hash_flatten): fix behavior of flatten(0).
      [ruby-dev:47988] [Bug #9533]
    
    * test/ruby/test_array.rb: test for above. patch is from
      Takeshi Sasaki.

  Modified files:
    trunk/hash.c
    trunk/test/ruby/test_hash.rb
Index: hash.c
===================================================================
--- hash.c	(revision 45058)
+++ hash.c	(revision 45059)
@@ -2374,18 +2374,25 @@ rb_hash_flatten(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/hash.c#L2374
 {
     VALUE ary;
 
-    ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
-    rb_hash_foreach(hash, flatten_i, ary);
     if (argc) {
 	int level = NUM2INT(*argv);
+	if (level == 0) return rb_hash_to_a(hash);
+
+	ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
+	rb_hash_foreach(hash, flatten_i, ary);
 	if (level - 1 > 0) {
 	    *argv = INT2FIX(level - 1);
 	    rb_funcall2(ary, id_flatten_bang, argc, argv);
 	}
-	else if (level == -1) {
+	else if (level < 0) {
 	    rb_funcall2(ary, id_flatten_bang, 0, 0);
 	}
     }
+    else {
+	ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
+	rb_hash_foreach(hash, flatten_i, ary);
+    }
+
     return ary;
 }
 
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 45058)
+++ test/ruby/test_hash.rb	(revision 45059)
@@ -974,7 +974,7 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L974
 
     a =  @cls[1=> "one", 2 => [2,"two"], 3 => [3, ["three"]]]
     assert_equal([1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten)
-    assert_equal([1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten(0))
+    assert_equal([[1, "one"], [2, [2, "two"]], [3, [3, ["three"]]]], a.flatten(0))
     assert_equal([1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten(1))
     assert_equal([1, "one", 2, 2, "two", 3, 3, ["three"]], a.flatten(2))
     assert_equal([1, "one", 2, 2, "two", 3, 3, "three"], a.flatten(3))

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

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