📅  最后修改于: 2023-12-03 15:27:30.260000             🧑  作者: Mango
The numerator() method is a built-in method in Ruby that is used to retrieve the numerator of a Rational class object. A Rational class object is used to represent a rational number as a pair of integers, namely the numerator and the denominator. The numerator() method returns the numerator of the rational number as an integer.
The syntax of the numerator() method is as follows:
rational.numerator()
In the above syntax, 'rational' is an object of the Rational class.
The numerator() method does not take any parameters.
The numerator() method returns an integer which is the numerator of the rational number represented by the object.
rational = Rational(4, 3)
num = rational.numerator()
puts "The numerator of the rational number #{rational} is #{num}"
The output of the above code would be:
The numerator of the rational number 4/3 is 4
In this tutorial, we learned about the numerator() method in Ruby which is used to retrieve the numerator of a rational number represented by an object of the Rational class. We also saw an example of how to use the numerator() method in a Ruby program.