summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-02-22 09:24:49 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-02-22 09:24:49 +0000
commitd2164fc5f2c8d32aaa9a857ca680c8f6d05ed106 (patch)
tree0676ba93527c8fcd6c04ec5c2efead54ad32a535 /hash.c
parent76ddadeb1ad5ba049efdd6463bf6169426127985 (diff)
merge revision(s) 45057,45059: [Backport #9533]
* hash.c (rb_hash_flatten): fix behavior of flatten(-1). [ruby-dev:47988] [Bug #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
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/hash.c b/hash.c
index 1c285c2b23..db510c818e 100644
--- a/hash.c
+++ b/hash.c
@@ -67,7 +67,7 @@ rb_hash_freeze(VALUE hash)
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)
@@ -2399,15 +2399,25 @@ rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
{
VALUE ary;
- 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 == 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 < 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;
}
@@ -3757,6 +3767,7 @@ Init_Hash(void)
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);