Open In App

Ruby | Rational rational() function()

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Rational() is an inbuilt method in Ruby returns the rational number of the form a/b.

Syntax: Rational(num, den)

Parameters: The function accepts two parameters which represents numerator and denominator. By default, the denominator is 1.

Return Value: It returns the rational number of the form a/b.

Example 1:




# Ruby program for Rational()
  
# Initialize rational number 
rat = Rational(4)
  
# Prints the rational number
puts rat


Output:

4/1

Example 2:




# Ruby program for Rational()
  
# Initialize rational number 
rat = Rational(4, 5)
  
# Prints the rational number
puts rat


Output:

4/5

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads