红宝石 |有理rational()函数()
Rational()是 Ruby 中的一个内置方法,以 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.
示例 1 :
# Ruby program for Rational()
# Initialize rational number
rat = Rational(4)
# Prints the rational number
puts rat
输出:
4/1
示例 2 :
# Ruby program for Rational()
# Initialize rational number
rat = Rational(4, 5)
# Prints the rational number
puts rat
输出:
4/5