红宝石 |整数 <=>函数
<=>是 Ruby 中的一个内置方法,它返回三个值 -1、0 或 +1。如果数字小于给定数字,则返回 -1,如果两者相同,则返回 0,如果大于给定数字,则返回 1。
Syntax: num1 <=> num2
Parameters: The function accepts no parameter.
Return Value: It returns -1, 0, +1 as stated above.
示例 1 :
# Ruby program for <=> method in Integer
# Initialize numbers
num1 = 6
num2 = 3
# Prints <=>
print num1 <=> num2
输出:
1
示例 2 :
# Ruby program for <=> method in Integer
# Initialize numbers
num1 = 2
num2 = 3
# Prints <=>
print num1 <=> num2
输出:
-1
示例 3 :
# Ruby program for <=> method in Integer
# Initialize numbers
num1 = 3
num2 = 3
# Prints <=>
print num1 <=> num2
输出:
0