红宝石 |数值方法
<=>是 Ruby 中的一个内置方法,如果两个数字相等则返回 0,否则返回 1。
Syntax: a <=> b
Parameters: The function needs two number whose comparison is to be done.
Return Value: It returns 0 if both the numbers are same, else it returns 1.
示例 1 :
Ruby
# Ruby program for <=> method in Numeric
# Initialize two numbers
a = 19
b = 4
# Prints 0 if equal else 1
puts a<=>b
Ruby
# Ruby program for <=> method in Numeric
# Initialize two numbers
a = 13
b = 13
# Prints 0 if equal else 1
puts a <=> b
输出:
1
示例 2 :
红宝石
# Ruby program for <=> method in Numeric
# Initialize two numbers
a = 13
b = 13
# Prints 0 if equal else 1
puts a <=> b
输出:
0