ruby-changes:51802
From: nobu <ko1@a...>
Date: Mon, 23 Jul 2018 01:04:04 +0900 (JST)
Subject: [ruby-changes:51802] nobu:r64015 (trunk): object.c: prefer base optarg
nobu 2018-07-23 01:03:58 +0900 (Mon, 23 Jul 2018) New Revision: 64015 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64015 Log: object.c: prefer base optarg * object.c (rb_f_integer): prefer `base` optional argument over keyword arguments. this issue should be resolved more generally by separating keyword arguments from hashes in the future. Modified files: trunk/object.c trunk/test/ruby/test_integer.rb Index: test/ruby/test_integer.rb =================================================================== --- test/ruby/test_integer.rb (revision 64014) +++ test/ruby/test_integer.rb (revision 64015) @@ -159,6 +159,12 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L159 assert_nothing_raised(TypeError) { assert_equal(nil, Integer(nil, exception: false)) } + + assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + def method_missing(*);"";end + assert_equal(0, Integer("0", 2)) + end; end def test_int_p Index: object.c =================================================================== --- object.c (revision 64014) +++ object.c (revision 64015) @@ -3214,15 +3214,23 @@ rb_f_integer(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/object.c#L3214 VALUE arg = Qnil, opts = Qnil; int base = 0; - switch (rb_scan_args(argc, argv, "11:", NULL, NULL, &opts)) { - case 2: - base = NUM2INT(argv[1]); - case 1: - arg = argv[0]; - break; - default: - UNREACHABLE; + if (argc > 1) { + int narg = 1; + VALUE vbase = rb_check_to_int(argv[1]); + if (!NIL_P(vbase)) { + base = NUM2INT(vbase); + narg = 2; + } + if (argc > narg) { + VALUE hash = rb_check_hash_type(argv[argc-1]); + if (!NIL_P(hash)) { + opts = rb_extract_keywords(&hash); + if (!hash) --argc; + } + } } + rb_check_arity(argc, 1, 2); + arg = argv[0]; return rb_convert_to_integer(arg, base, opts_exception_p(opts)); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/