diff --git a/object.c b/object.c index 9492abfac9..7d6e47932c 100644 --- a/object.c +++ b/object.c @@ -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)); } diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index 0b9aa4191d..51737d3d1f 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -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