Ruby 数组<=>函数

Ruby 数组<=>函数

<=>是一个数组类的方法,它在两个数组之间进行比较。

语法。<=>

参数。用于比较的数组

返回。1 : 如果a > b

-1 : 如果a < b

0 : 如果a = b

例子 #1 :

# Ruby code for <=>() method 
# checking equality 
     
# declaring arrays 
a = [18, 22, 33, 4, 5, 6] 
     
# declaring arrays 
b = [18, 22, 33, 4, 5, 6] 
     
# declaring arrays 
c = [18, 22, 33, 40, 50, 6] 
     
# <=> method
puts "<=> method : #{a <=> b}\n\n"
     
# <=> method 
puts "<=> method : #{a <=> c}\n\n"
     
# <=> method
puts "<=> method : #{b <=> c}\n\n"

输出:

 method : 0

 method : -1

 method : -1

例子 #2 :

# Ruby code for <=>() method 
# checking equality 
     
# declaring arrays 
a = ["abc", "xyz", "dog"] 
     
# declaring arrays 
b = ["cat", "cat", "dog"] 
     
# declaring arrays 
c = ["cat", "cat", "dog"] 
     
# <=> method
puts "<=> method : #{a <=> b}\n\n"
     
# <=> method
puts "<=> method : #{a <=> c}\n\n"
     
# <=> method
puts "<=> method : #{b <=> c}\n\n"

输出:

 method : -1

 method : -1

 method : 0

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程