ruby-changes:33780
From: nobu <ko1@a...>
Date: Wed, 7 May 2014 17:24:15 +0900 (JST)
Subject: [ruby-changes:33780] nobu:r45861 (trunk): numeric.c: check keyword arguments
nobu 2014-05-07 17:24:09 +0900 (Wed, 07 May 2014) New Revision: 45861 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45861 Log: numeric.c: check keyword arguments * numeric.c (num_step_scan_args): check keyword arguments and fail if they conflict with positional arguments. [ruby-dev:48177] [Bug #9811] Modified files: trunk/ChangeLog trunk/numeric.c trunk/test/ruby/test_numeric.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 45860) +++ ChangeLog (revision 45861) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed May 7 17:24:07 2014 Nobuyoshi Nakada <nobu@r...> + + * numeric.c (num_step_scan_args): check keyword arguments and fail + if they conflict with positional arguments. + [ruby-dev:48177] [Bug #9811] + Wed May 7 12:06:14 2014 Koichi Sasada <ko1@a...> * benchmark/driver.rb: remove debug output and output results into Index: numeric.c =================================================================== --- numeric.c (revision 45860) +++ numeric.c (revision 45861) @@ -1867,8 +1867,19 @@ num_step_scan_args(int argc, const VALUE https://github.com/ruby/ruby/blob/trunk/numeric.c#L1867 argc = rb_scan_args(argc, argv, "02:", to, step, &hash); if (!NIL_P(hash)) { - *step = rb_hash_aref(hash, sym_by); - *to = rb_hash_aref(hash, sym_to); + ID keys[2]; + VALUE values[2]; + keys[0] = sym_to; + keys[1] = sym_by; + rb_get_kwargs(hash, keys, 0, 2, values); + if (values[0] != Qundef) { + if (argc > 0) rb_raise(rb_eArgError, "to is given twice"); + *to = values[0]; + } + if (values[1] != Qundef) { + if (argc > 1) rb_raise(rb_eArgError, "step is given twice"); + *step = values[1]; + } } else { /* compatibility */ Index: test/ruby/test_numeric.rb =================================================================== --- test/ruby/test_numeric.rb (revision 45860) +++ test/ruby/test_numeric.rb (revision 45861) @@ -268,6 +268,14 @@ class TestNumeric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_numeric.rb#L268 assert_nothing_raised { 1.step(by: nil) } assert_nothing_raised { 1.step(by: nil).size } + bug9811 = '[ruby-dev:48177] [Bug #9811]' + assert_raise(ArgumentError, bug9811) { 1.step(10, foo: nil) {} } + assert_raise(ArgumentError, bug9811) { 1.step(10, foo: nil).size } + assert_raise(ArgumentError, bug9811) { 1.step(10, to: 11) {} } + assert_raise(ArgumentError, bug9811) { 1.step(10, to: 11).size } + assert_raise(ArgumentError, bug9811) { 1.step(10, 1, by: 11) {} } + assert_raise(ArgumentError, bug9811) { 1.step(10, 1, by: 11).size } + assert_equal(bignum*2+1, (-bignum).step(bignum, 1).size) assert_equal(bignum*2, (-bignum).step(bignum-1, 1).size) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/