📜  Ruby Array()函数示例

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

Ruby Array()函数示例

Array#() : ()是一个 Array 类方法,用于执行两个数组之间的比较。

示例 #1:

# Ruby code for <=>() method
# showing comparison
  
# declaring array
a = [18, 22, 33, 4, 5, 6]
  
# declaring array
b = [5, 4, 22, 1, 88, 9]
  
# declaring array
c = [18, 22, 33, 40, 50, 6]
  
# 1 as a>b
puts "comparison : #{a <=> b}\n\n"
  
# -1 as a c}\n\n"
  
# -1 as b c}\n\n"

输出 :

comparison : 1

comparison : -1

comparison : -1

示例 #2:

# Ruby code for <=>() method
# showing comparison
  
# declaring array
a = ["abc", "xyz", "dog"]
  
# declaring array
b = ["cow", "cat", "dog"]
  
# declaring array
c = ["cat", "1", "dog"]
  
# -1 as a b}\n\n"
  
# -1 as a c}\n\n"
  
# 1 as b>c 
puts "comparison : #{b <=> c}\n\n"

输出 :

comparison : -1

comparison : -1

comparison : 1