红宝石 |有理 <=>函数
<=>是 Ruby 中的内置方法,根据有理值是小于、等于还是大于数值返回 -1、0 或 +1。如果两个值不可比较,则返回nil 。
Syntax: rat1<=>rat2
Parameters: The function accepts no parameter
Return Value: It returns -1, 0, or +1 depending on whether rational is less than, equal to, or greater than the numeric value.
示例 1 :
Ruby
# Ruby program for <=> method
# Initialize rational number
rat1 = Rational(1, 3)
rat2 = Rational(1, 3)
# Prints the rational number
puts rat1 <=> rat2
Ruby
# Ruby program for <=> method
# Initialize rational number
rat1 = Rational(1, 3)
# Prints the rational number
puts rat1 <=> 0.3
输出:
0
示例 2 :
红宝石
# Ruby program for <=> method
# Initialize rational number
rat1 = Rational(1, 3)
# Prints the rational number
puts rat1 <=> 0.3
输出:
1