

Ruby 3.1 adds try_convert method to Integer class for implicit conversions
source link: https://blog.saeloun.com/2021/08/03/ruby-adds-integer-try-convert
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Ruby 3.1 adds try_convert method to Integer class for implicit conversions
Aug 3, 2021 , by Jijo Bose 1 minute readImplicit and Explicit Conversion
Ruby objects are usually converted to other classes/types using to_*
functions.
For example, converting the string “42” to an integer is done with to_i
.
The following table shows the defined conversion methods of Ruby’s important core classes:
Class
Explicit Conversion
Implicit Conversion
Array
to_a
to_ary
Hash
to_h
to_hash
String
to_s
to_str
Integer
to_i
to_int
Float
to_f
-
Complex
to_c
-
Rational
to_r
-
Regexp
-
to_regexp
IO
-
to_io
Before
Some of Ruby’s core classes like String,
Array,
Hash,
Regexp has a try_convert
class method,
but it was missing in the case of Integer.
After
Starting with Ruby 3.1
try_convert
method is added to Integer class.
It converts the argument by to_int method without explicit conversions
like Integer()
.
try_convert
method converts the object into
an instance of the Integer class using the implicit conversion method to_int
.
If no implicit conversion method is defined,
it returns nil.
It also returns nil,
if the result of the conversion is nil,
or nil was passed as an argument.
Below is an example of how the try_convert method can be used in the Integer
x = 42
Integer.try_convert(x) # 42
# returns the argument if it's an Integer
x = "42"
Integer.try_convert(x) # nil
# returns nil when the argument does not respond to to_int
obj = Object.new
# sends to_int to the argument and raises TypeError if it's not a kind of Integer
Integer.try_convert(obj)
#=> TypeError: can't convert Object to Integer
obj = Object.new
# explicit conversion using to_i
def obj.to_i
1
end
# returns nil because `to_i` performs explicit conversion.
Integer.try_convert(obj)
#=> nil
obj = Object.new
# implicit conversion using to_int
def obj.to_int
1
end
# returns 1 because `to_int` performs implicit conversion.
Integer.try_convert(obj)
#=> 1
obj = Object.new
# implicit conversion using to_int, but we are not returning an integer.
def obj.to_int
Object.new
end
Integer.try_convert(obj)
#=> TypeError: can't convert Object to Integer
# Raises as error, as return type is not an integer.
When to Use What?
- Use
to_*
methods for explicit conversion. - Use
.try_convert
for implicit conversion.
Recommend
-
44
If you've ever come across a situation to convert your grandfathers' Java project (which he used to build with Ant) to Maven, you know my pain. Maybe you are reading this because it's time to do that. So, here is a defini...
-
5
Implicit vs explicit type conversions in Ruby (to_h/to_hash and others) Jan 18, 2016 Just a quick reference post. There are several pairs of type coercions methods in Ruby: to_i and
-
12
Convert the Boolean to Int: Here in this article, we learn how to convert Boolean to an integer value in JavaScript. While programming when we need to work with Yes/No, ON/OFF, or true/false values, we use Boolean datatype. As we know...
-
7
Implicit Conversions In Scala: Let’s extend Functionality Reading Time: 3 minutes Hello folks, in this blog we will see Im...
-
4
Knolx: Extractors & Implicit conversions Reading Time: < 1 minuteIn this presentation, I have explained few feature...
-
6
Convert a sentence to integer advertisements Assuming i have a string that says string version = "Version 1.0.2.3"
-
27
Microsoft Visual Studio 2022 and Floating-point to Integer Conversions December 13th, 2021 Microsoft Visual Studio 2022 and Floating-point to Integer Convers...
-
9
F# has always been quite strict with its types, and that's broadly very useful. Implicit conversions and upcasting are often not allowed, helping to prevent certain kinds of bugs. However, in F# 6 some of these rules have been relaxed.
-
12
[Golang] Convert Integer to String via fmt.Sprintf May 28, 2018
-
6
Implicit, to be or not to be Implicit, to be or not to be Table of Contents 隐式转...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK