📜  红宝石 |数值 numerator()函数(1)

📅  最后修改于: 2023-12-03 15:27:30.260000             🧑  作者: Mango

Ruby | numerator() method

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.

Syntax

The syntax of the numerator() method is as follows:

rational.numerator()

In the above syntax, 'rational' is an object of the Rational class.

Parameters

The numerator() method does not take any parameters.

Return Value

The numerator() method returns an integer which is the numerator of the rational number represented by the object.

Example
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
Conclusion

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.