Skip to content

Commit ada0c63

Browse files
committed
merge revision(s) 45057,45059: [Backport ruby#9533]
* hash.c (rb_hash_flatten): fix behavior of flatten(-1). [ruby-dev:47988] [Bug ruby#9533] * test/ruby/test_array.rb: test for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@45126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent e16f765 commit ada0c63

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

ChangeLog

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Sat Feb 22 18:20:58 2014 Masaki Matsushita <glass.saga@gmail.com>
2+
3+
* hash.c (rb_hash_flatten): fix behavior of flatten(-1).
4+
[ruby-dev:47988] [Bug #9533]
5+
6+
* test/ruby/test_array.rb: test for above.
7+
18
Sat Feb 22 17:46:32 2014 Tanaka Akira <akr@fsij.org>
29

310
* lib/open-uri.rb: Make proxy disabling working again.

hash.c

+18-7
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ rb_hash_freeze(VALUE hash)
6767
VALUE rb_cHash;
6868

6969
static VALUE envtbl;
70-
static ID id_hash, id_yield, id_default;
70+
static ID id_hash, id_yield, id_default, id_flatten_bang;
7171

7272
VALUE
7373
rb_hash_set_ifnone(VALUE hash, VALUE ifnone)
@@ -2399,15 +2399,25 @@ rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
23992399
{
24002400
VALUE ary;
24012401

2402-
ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
2403-
rb_hash_foreach(hash, flatten_i, ary);
24042402
if (argc) {
2405-
int level = NUM2INT(*argv) - 1;
2406-
if (level > 0) {
2407-
*argv = INT2FIX(level);
2408-
rb_funcall2(ary, rb_intern("flatten!"), argc, argv);
2403+
int level = NUM2INT(*argv);
2404+
if (level == 0) return rb_hash_to_a(hash);
2405+
2406+
ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
2407+
rb_hash_foreach(hash, flatten_i, ary);
2408+
if (level - 1 > 0) {
2409+
*argv = INT2FIX(level - 1);
2410+
rb_funcall2(ary, id_flatten_bang, argc, argv);
24092411
}
2412+
else if (level < 0) {
2413+
rb_funcall2(ary, id_flatten_bang, 0, 0);
2414+
}
2415+
}
2416+
else {
2417+
ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
2418+
rb_hash_foreach(hash, flatten_i, ary);
24102419
}
2420+
24112421
return ary;
24122422
}
24132423

@@ -3757,6 +3767,7 @@ Init_Hash(void)
37573767
id_hash = rb_intern("hash");
37583768
id_yield = rb_intern("yield");
37593769
id_default = rb_intern("default");
3770+
id_flatten_bang = rb_intern("flatten!");
37603771

37613772
rb_cHash = rb_define_class("Hash", rb_cObject);
37623773

test/ruby/test_hash.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -973,11 +973,11 @@ def test_flatten
973973

974974
a = @cls[1=> "one", 2 => [2,"two"], 3 => [3, ["three"]]]
975975
assert_equal([1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten)
976-
assert_equal([1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten(-1))
977-
assert_equal([1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten(0))
976+
assert_equal([[1, "one"], [2, [2, "two"]], [3, [3, ["three"]]]], a.flatten(0))
978977
assert_equal([1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten(1))
979978
assert_equal([1, "one", 2, 2, "two", 3, 3, ["three"]], a.flatten(2))
980979
assert_equal([1, "one", 2, 2, "two", 3, 3, "three"], a.flatten(3))
980+
assert_equal([1, "one", 2, 2, "two", 3, 3, "three"], a.flatten(-1))
981981
assert_raise(TypeError){ a.flatten(Object) }
982982
end
983983

version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define RUBY_VERSION "2.1.1"
22
#define RUBY_RELEASE_DATE "2014-02-22"
3-
#define RUBY_PATCHLEVEL 70
3+
#define RUBY_PATCHLEVEL 71
44

55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 2

0 commit comments

Comments
 (0)