Related Articles

Related Articles

Ruby | Rational rationalize() function
  • Last Updated : 06 Jan, 2020

The rationalize() is an inbuilt function in Ruby returns a simpler approximation of the value if the optional argument eps is given (rat-|eps| <= result <= rat+|eps|) otherwise returns its self.

Syntax: rat.rationalize(eps)

Parameters: The function accepts a single optional parameter

Return Value: It returns a simpler approximation of the value if the optional argument eps is given (rat-|eps| <= result <= rat+|eps|) otherwise returns its self

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for rationalize() method
  
# Initialize rational number
rat1 = Rational(123, 456)
  
# Prints the rational number
puts rat1.rationalize

chevron_right


Output:

41/152

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for rationalize() method
  
# Initialize rational number
rat1 = Rational(123, 456)
  
# Prints the rational number
puts rat1.rationalize(Rational('.001'))

chevron_right


Output:

7/26
My Personal Notes arrow_drop_up
Recommended Articles
Page :