Does Ruby has a method I could use when I have 2 arrays (lists) and I want to get an array (list) of only the values common 开发者_开发问答to both arrays? Like this..
a = [1,2,3]
b = [3,4,5]
=> the method would return [3]
And the other way around, values that are "unique" in those arrays (lists).
a = [1,2,3]
b = [3,4,5]
=> the method would return [1,2,4,5]
AND : a & b
There are no XOR method for arrays in Ruby, so you may do it via another methods. Here are 2 ways:
XOR : (a | b) - (a & b)
XOR : (a + b) - (a & b) # this result can have duplicates!
XOR : (a - b) | (b - a)
XOR : (a - b) + (b - a) # this result can have duplicates!
The words you are looking for are intersection and symmetric difference. AFAIK it's this in Ruby:
[1,2,3] & [3,4,5] = [3]
[1,2,3] ^ [3,4,5] = [1,2,4,5]
精彩评论