We use the to_r
Integer instance method to convert an integer to a rational.
integer.to_r
This method does not take any parameters.
This method returns the integer
as a rational.
Let’s look at the code below:
# create some integer valuesint1 = 2int2 = 3**4int3 = 4*5-2int4 = 100# convert to rationals# and print resultsputs int1.to_rputs int2.to_rputs int3.to_rputs int4.to_r
_r
method to convert instances to rationals and the values are printed to the console.