From 2cfc5b03dac80a92de2dc7a17be4b3cfff92adf7 Mon Sep 17 00:00:00 2001 From: mrkn Date: Thu, 15 Mar 2018 07:19:43 +0000 Subject: Add `exception:` keyword in Kernel#Integer() Support `exception:` keyword argument in Kernel#Integer(). If `exception:` is `false`, `Kernel#Integer()` returns `nil` if the given value cannot be interpreted as an integer value. The default value of `exception:` is `true`. This is part of [Feature #12732]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- bignum.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'bignum.c') diff --git a/bignum.c b/bignum.c index 6c40c778de..b4c7560034 100644 --- a/bignum.c +++ b/bignum.c @@ -4230,7 +4230,7 @@ rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base) } VALUE -rb_str_to_inum(VALUE str, int base, int badcheck) +rb_str_convert_to_inum(VALUE str, int base, int badcheck, int raise_exception) { VALUE ret; const char *s; @@ -4242,12 +4242,21 @@ rb_str_to_inum(VALUE str, int base, int badcheck) RSTRING_GETMEM(str, s, len); ret = rb_cstr_parse_inum(s, len, (badcheck ? NULL : &end), base); if (NIL_P(ret)) { - if (badcheck) invalid_integer(str); - ret = INT2FIX(0); + if (badcheck) { + if (!raise_exception) return Qnil; + invalid_integer(str); + } + ret = INT2FIX(0); } return ret; } +VALUE +rb_str_to_inum(VALUE str, int base, int badcheck) +{ + return rb_str_convert_to_inum(str, base, badcheck, TRUE); +} + VALUE rb_str2big_poweroftwo(VALUE arg, int base, int badcheck) { -- cgit v1.2.1