红宝石 |有理 to_s()函数
to_s()是 Ruby 中的内置函数,以字符串形式返回值
Syntax: rat.to_s()
Parameters: The function accepts no parameter
Return Value: It returns value as string
示例 1 :
#Ruby program for to_s() method
#Initialize rational number
rat1 = Rational(18, -4)
#Prints the rational number
puts rat1.to_s()
输出:
-9/2
示例 2 :
#Ruby program for to_s() method
#Initialize rational number
rat1 = Rational(7)
#Prints the rational number
puts rat1.to_s()
输出:
7/1