Array Set Operations in Ruby

Do you ever find yourself doing this?


In both cases, we have an Array we want to use as a set, containing only unique elements.

One way to tackle this more cleanly is to simply use a Set.

But the Set and Array interfaces differ in some regards, and if other code is already expecting the collection to be an Array, that solution may not be practical.

As it happens, Array supports several basic set operations innately. You may already know about these, but in case you don’t, here are some examples.

Set union:

Set difference:

Set intersection:

It’s a small thing, but perhaps it will save you a few lines of code.

UPDATE: My WordPress “related posts” feature points out that I have officially begun to repeat myself. Ah well. If nothing else this article has a bit more explanation than the one from 2010.