mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45059 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2653f23858
commit
13834fb3cb
2 changed files with 11 additions and 4 deletions
13
hash.c
13
hash.c
|
@ -2374,18 +2374,25 @@ rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
|
||||||
{
|
{
|
||||||
VALUE ary;
|
VALUE ary;
|
||||||
|
|
||||||
ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
|
|
||||||
rb_hash_foreach(hash, flatten_i, ary);
|
|
||||||
if (argc) {
|
if (argc) {
|
||||||
int level = NUM2INT(*argv);
|
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) {
|
if (level - 1 > 0) {
|
||||||
*argv = INT2FIX(level - 1);
|
*argv = INT2FIX(level - 1);
|
||||||
rb_funcall2(ary, id_flatten_bang, argc, argv);
|
rb_funcall2(ary, id_flatten_bang, argc, argv);
|
||||||
}
|
}
|
||||||
else if (level == -1) {
|
else if (level < 0) {
|
||||||
rb_funcall2(ary, id_flatten_bang, 0, 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;
|
return ary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -974,7 +974,7 @@ class TestHash < Test::Unit::TestCase
|
||||||
|
|
||||||
a = @cls[1=> "one", 2 => [2,"two"], 3 => [3, ["three"]]]
|
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)
|
||||||
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(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(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(3))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue