Set Operations on Arrays in Ruby
Arrays in Ruby allow you to find union, intersections, Cartesian product and difference in two arrays. Ruby’s Arrays have overloaded arithmetic as well as logical operators to with union, intersections, Cartesian products. Here is a list of examples
[1,2,3] | [1,5,6] // will produce [1,2,3,5,6] or union of two arrays
[1,2,3] & [1,5,6] //will produce [1] of intersection of two arrays
[1,2,3] - [1,5,6] //will produce difference of two arrays
Difference in two arrays can be applied to array of strings as in the following example.
[:one, :two, :five] – [:one] // will produce [:two, :five]