Ruby 数组差分(-)函数
Array#-()是一个数组类的方法,它通过删除两个数组中的相似元素来执行集合差异操作。
语法。Array.-()
参数。用于执行连接操作的数组。
返回。通过删除两个数组中的相同元素而产生的新数组。
例子 #1 :
# Ruby code for -() method
# showing concatenate operation
# 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]
# differencing arrays
puts "difference of a and b : #{a - b}\n\n"
# differencing arrays
puts "difference of a and c : #{a - c}\n\n"
# differencing arrays
puts "difference of b and c : #{b - c}\n\n"
输出:
difference of a and b : [18, 33, 6]
difference of a and c : [4, 5]
difference of b and c : [5, 4, 1, 88, 9]
例子 #2 :
# Ruby code for -() method
# showing concatenate operation
# declaring array
a = ["abc", "xyz", "dog"]
# declaring array
b = ["cow", "cat", "dog"]
# declaring array
c = ["cat", "1", "dog"]
# differencing arrays
puts "difference of a and b : #{a - b}\n\n"
# differencing arrays
puts "difference of a and c : #{a - c}\n\n"
# differencing arrays
puts "difference of b and c : #{b - c}\n\n"
输出:
difference of a and b : ["abc", "xyz"]
difference of a and c : ["abc", "xyz"]
difference of b and c : ["cow"]