1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64015 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-07-22 16:03:58 +00:00
parent 67cacdb836
commit 02cae85e34
2 changed files with 22 additions and 8 deletions

View file

@ -3214,15 +3214,23 @@ rb_f_integer(int argc, VALUE *argv, VALUE obj)
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));
}

View file

@ -159,6 +159,12 @@ class TestInteger < Test::Unit::TestCase
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