aboutsummaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-22 00:38:05 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-22 00:38:05 +0000
commit61ef7095822e24ce5983b2369fbead734109f48c (patch)
tree33bfa3d96eba43f45d7356902af2a98254cf0bee /hash.c
parentd96152634509be95851102e7d8fee547aa4826c0 (diff)
downloadruby-61ef7095822e24ce5983b2369fbead734109f48c.tar.gz
Add arity check into Hash#flatten
* hash.c (rb_hash_flatten): add arity check git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60335 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/hash.c b/hash.c
index c0f2252948..ecc9110d58 100644
--- a/hash.c
+++ b/hash.c
@@ -2787,17 +2787,23 @@ rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
{
VALUE ary;
+ rb_check_arity(argc, 0, 1);
+
if (argc) {
- int level = NUM2INT(*argv);
+ int level = NUM2INT(argv[0]);
+
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_funcallv(ary, id_flatten_bang, argc, argv);
+ level--;
+
+ if (level > 0) {
+ VALUE ary_flatten_level = INT2FIX(level);
+ rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
}
else if (level < 0) {
+ /* flatten recursively */
rb_funcallv(ary, id_flatten_bang, 0, 0);
}
}