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

ruby-changes:32978

From: glass <ko1@a...>
Date: Thu, 20 Feb 2014 11:27:34 +0900 (JST)
Subject: [ruby-changes:32978] glass:r45057 (trunk): * hash.c (rb_hash_flatten): fix behavior of flatten(-1).

glass	2014-02-20 11:27:30 +0900 (Thu, 20 Feb 2014)

  New Revision: 45057

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

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

  Modified files:
    trunk/ChangeLog
    trunk/hash.c
    trunk/test/ruby/test_hash.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45056)
+++ ChangeLog	(revision 45057)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Feb 20 11:21:13 2014  Masaki Matsushita  <glass.saga@g...>
+
+	* hash.c (rb_hash_flatten): fix behavior of flatten(-1).
+	  [ruby-dev:47988] [Bug #9533]
+
+	* test/ruby/test_array.rb: test for above.
+
 Wed Feb 19 18:57:02 2014  Tanaka Akira  <akr@f...>
 
 	* ext/socket: Bypass getaddrinfo() if node and serv are numeric.
Index: hash.c
===================================================================
--- hash.c	(revision 45056)
+++ hash.c	(revision 45057)
@@ -66,7 +66,7 @@ rb_hash_freeze(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L66
 VALUE rb_cHash;
 
 static VALUE envtbl;
-static ID id_hash, id_yield, id_default;
+static ID id_hash, id_yield, id_default, id_flatten_bang;
 
 VALUE
 rb_hash_set_ifnone(VALUE hash, VALUE ifnone)
@@ -2377,10 +2377,13 @@ rb_hash_flatten(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/hash.c#L2377
     ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
     rb_hash_foreach(hash, flatten_i, ary);
     if (argc) {
-	int level = NUM2INT(*argv) - 1;
-	if (level > 0) {
-	    *argv = INT2FIX(level);
-	    rb_funcall2(ary, rb_intern("flatten!"), argc, argv);
+	int level = NUM2INT(*argv);
+	if (level - 1 > 0) {
+	    *argv = INT2FIX(level - 1);
+	    rb_funcall2(ary, id_flatten_bang, argc, argv);
+	}
+	else if (level == -1) {
+	    rb_funcall2(ary, id_flatten_bang, 0, 0);
 	}
     }
     return ary;
@@ -3732,6 +3735,7 @@ Init_Hash(void) https://github.com/ruby/ruby/blob/trunk/hash.c#L3735
     id_hash = rb_intern("hash");
     id_yield = rb_intern("yield");
     id_default = rb_intern("default");
+    id_flatten_bang = rb_intern("flatten!");
 
     rb_cHash = rb_define_class("Hash", rb_cObject);
 
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 45056)
+++ test/ruby/test_hash.rb	(revision 45057)
@@ -974,11 +974,11 @@ 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(-1))
     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))
+    assert_equal([1, "one", 2, 2, "two", 3, 3, "three"], a.flatten(-1))
     assert_raise(TypeError){ a.flatten(Object) }
   end
 

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

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