summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2018-10-07 13:02:46 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-07-16 17:49:53 +0900
commit301d194ee3b49e6b078eccb999dd538e9bfa8c7c (patch)
tree8488af78fa5c0f0dab79905a577e365acef7ca01 /object.c
parenteee709595cecdbc35d7bca9e779c36523c4659c5 (diff)
Add Integer.try_convert [Feature #15211]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4654
Diffstat (limited to 'object.c')
-rw-r--r--object.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/object.c b/object.c
index 695a11e366..43c97272dd 100644
--- a/object.c
+++ b/object.c
@@ -3232,19 +3232,23 @@ rb_check_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
#define try_to_int(val, mid, raise) \
convert_type_with_id(val, "Integer", mid, raise, -1)
-ALWAYS_INLINE(static VALUE rb_to_integer(VALUE val, const char *method, ID mid));
+ALWAYS_INLINE(static VALUE rb_to_integer_with_id_exception(VALUE val, const char *method, ID mid, int raise));
+/* Integer specific rb_check_convert_type_with_id */
static inline VALUE
-rb_to_integer(VALUE val, const char *method, ID mid)
+rb_to_integer_with_id_exception(VALUE val, const char *method, ID mid, int raise)
{
VALUE v;
if (RB_INTEGER_TYPE_P(val)) return val;
- v = try_to_int(val, mid, TRUE);
+ v = try_to_int(val, mid, raise);
+ if (!raise && NIL_P(v)) return Qnil;
if (!RB_INTEGER_TYPE_P(v)) {
conversion_mismatch(val, "Integer", method, v);
}
return v;
}
+#define rb_to_integer(val, method, mid) \
+ rb_to_integer_with_id_exception(val, method, mid, TRUE)
/**
* Tries to convert \a val into \c Integer.
@@ -3371,6 +3375,12 @@ rb_Integer(VALUE val)
return rb_convert_to_integer(val, 0, TRUE);
}
+VALUE
+rb_check_integer_type(VALUE val)
+{
+ return rb_to_integer_with_id_exception(val, "to_int", idTo_int, FALSE);
+}
+
int
rb_bool_expected(VALUE obj, const char *flagname)
{