Ruby supports bitwise operators. Most languages support these operators. But most guides I've seen demonstrate these operators with binary values. What if we apply these in Ruby to arrays?

If we have two arrays containing numbers and we want to create a third array with the contents of the two arrays merged together? We can do this with the binary OR operator:

[1,2,3]|[3,4,5]=>[1,2,3,4,5]

Notice that the new array contains all values from both, but does not duplicate the 3. The binary OR operator will always copy what exists in either operand (in this case array), but will not duplicate entries.

The AND operator is useful as well. Let's say we want to only copy values that match:

[1,2,3]&[3,4,5]=>[3]

Only the 3 matches in both operands, so only it gets copied. What if we want to only copy stuff from the first array that are not in the second array:

[1,2,3]-[3,4,5]=>[1,2]

Here's a slightly more interesting one. What if I want all items merged from both, except for the matching ones? In other words, I want all the numbers except the common 3. Here's how:

a,b=[1,2,3],[3,4,5]a-b|b-a=>[1,2,4,5]

As you can see, that gives us all non-matching elements from both sets.

All of this is done without the need for if statements. I could have written a bunch of loops to create the datasets that I needed, and it would have taken a lot of looping and if statements. Instead, I'm making use of operators on the arrays.

What about lists of objects? We can do the same thing! If we have the following class, we can apply these same operations to the objects, using a property of the object:

While I'm using the name, I could have just as easily had an ID attribute on the person and used that instead. Using operators is a very easy and quick way to get unions and intersects of various sets.