Open In App

Ruby Integer to_r function with example

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The to_r function in Ruby converts the value of the number to a rational.

Syntax: number.to_r

Parameter: The function takes the number which is to be converted to a rational number.

Return Value: The function returns the rational number.

Example #1:




# Ruby program for to_r function 
   
# Initializing the number
num1 = 51
num2 = 10.78
num3 = 1690.89
num4 = 183
    
# Prints the number as rational
puts num1.to_r
puts num2.to_r
puts num3.to_r
puts num4.to_r


Output:

51/1
6068600497881743/562949953421312
7436612865160643/4398046511104
183/1

Example #2:




# Ruby program for to_r function 
   
# Initializing the number
num1 = 312
num2 = 98.09
num3 = 190.23
num4 = 2.5
    
# Prints the number as rational
puts num1.to_r
puts num2.to_r
puts num3.to_r
puts num4.to_r


Output:

 312/1
3451235058193531/35184372088832
6693123102458511/35184372088832
5/2

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads