ruby-changes:48220
From: glass <ko1@a...>
Date: Sun, 22 Oct 2017 09:38:10 +0900 (JST)
Subject: [ruby-changes:48220] glass:r60335 (trunk): Add arity check into Hash#flatten
glass 2017-10-22 09:38:05 +0900 (Sun, 22 Oct 2017) New Revision: 60335 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60335 Log: Add arity check into Hash#flatten * hash.c (rb_hash_flatten): add arity check Modified files: trunk/hash.c trunk/test/ruby/test_hash.rb Index: hash.c =================================================================== --- hash.c (revision 60334) +++ hash.c (revision 60335) @@ -2787,17 +2787,23 @@ rb_hash_flatten(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/hash.c#L2787 { 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); } } Index: test/ruby/test_hash.rb =================================================================== --- test/ruby/test_hash.rb (revision 60334) +++ test/ruby/test_hash.rb (revision 60335) @@ -1118,7 +1118,12 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L1118 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) } + assert_raise(TypeError){ a.flatten(nil) } + end + + def test_flatten_arity + a = @cls[1=> "one", 2 => [2,"two"], 3 => [3, ["three"]]] + assert_raise(ArgumentError){ a.flatten(1, 2) } end def test_callcc -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/