Open In App

Ruby | Rational rational() function()

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
Article Tags :