ruby-changes:70107
From: Nobuyoshi <ko1@a...>
Date: Wed, 8 Dec 2021 18:00:52 +0900 (JST)
Subject: [ruby-changes:70107] 454b4da763 (master): [DOC] Integer.try_convert [ci skip]
https://git.ruby-lang.org/ruby.git/commit/?id=454b4da763 From 454b4da7635b6322e85fd38735d9a04963644c4b Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Wed, 8 Dec 2021 17:59:16 +0900 Subject: [DOC] Integer.try_convert [ci skip] --- numeric.c | 1 + numeric.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/numeric.c b/numeric.c index 28b68a71fbd..d6fe05d8e81 100644 --- a/numeric.c +++ b/numeric.c @@ -5991,6 +5991,7 @@ rb_int_s_isqrt(VALUE self, VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5991 } } +/* :nodoc: */ static VALUE int_s_try_convert(VALUE self, VALUE num) { diff --git a/numeric.rb b/numeric.rb index 08b31ac3076..d48a4ee336b 100644 --- a/numeric.rb +++ b/numeric.rb @@ -229,6 +229,25 @@ class Integer https://github.com/ruby/ruby/blob/trunk/numeric.rb#L229 end end +# call-seq: +# Integer.try_convert(object) -> object, integer, or nil +# +# If +object+ is an \Integer object, returns +object+. +# +# Otherwise if +object+ responds to <tt>:to_int</tt>, +# calls <tt>object.to_int</tt> and returns the result. +# +# Returns +nil+ if +object+ does not respond to <tt>:to_ary</tt> +# +# Raises an exception unless <tt>object.to_int</tt> returns an \Integer object. +# +def Integer.try_convert(num) +=begin + Primitive.attr! 'inline' + Primitive.cexpr! 'rb_check_integer_type(num)' +=end +end if false + class Float # # call-seq: -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/