📜  红宝石 |数组 &()函数

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

红宝石 |数组 &()函数

Array#&() : &()是一个 Array 类方法,它返回一个新数组,其中包含两个数组共有的唯一元素。

示例 #1:

# Ruby code for &() method 
     
# 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 form : #{a & b}\n\n"
     
# & method
puts "& method form : #{a & c}\n\n"
     
# & method 
puts "& method form : #{b & c}\n\n"

输出 :

& method form : [18, 22, 33, 4, 5, 6]

& method form : [18, 22, 33, 6]

& method form : [18, 22, 33, 6]

示例 #2:

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

输出 :

& method form : ["dog"]

& method form : ["dog"]

& method form : ["cat", "dog"]