红宝石 |有理 to_i()函数
to_i()是 Ruby 中的内置函数,返回截断的整数值
Syntax: rat.to_i()
Parameters: The function accepts no parameter
Return Value: It returns the truncated integer value
示例 1 :
#Ruby program for to_i() method
#Initialize rational number
rat1 = Rational(9, -2)
#Prints the rational number
puts rat1.to_i()
输出:
-4
示例 2 :
#Ruby program for to_i() method
#Initialize rational number
rat1 = Rational(7)
#Prints the rational number
puts rat1.to_i()
输出:
7