📜  红宝石 |有理 <=>函数

📅  最后修改于: 2022-05-13 01:55:20.137000             🧑  作者: Mango

红宝石 |有理 <=>函数

<=>是 Ruby 中的内置方法,根据有理值是小于、等于还是大于数值返回 -1、0 或 +1。如果两个值不可比较,则返回nil

示例 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