Using a Set in Ruby

A Set 1 is a useful collection and something I under-utilize in Ruby. A Set does not allow duplicates. Converting between a Set and an Array is easy. In the article A Guide to Ruby Collections, II: Hashes, Sets, and Ranges 2 the author has listed some nice examples where Sets are useful.

(Set.new([1,2]) + [2]).to_a
=> [1, 2]

Equality of Sets can be determined with #eql? (the "identity" value, same as triple equals) and by comparing the #hash value. The order of the items is not part of the equality check. The second example shows another style of constructing a Set.

Set.new([1,2]).eql?(Set.new[2,1]) # => true
Set[1,2].hash == Set[2,1].hash # => true

Sets can be used to store different types of objects like Array. Set operations can be performed which might make cleaner code when finding a subset of values. More Set operations can be found in the article When Is a Set Better Than an Array in Ruby? 3.

Set operations

Set operations can also be performed on Arrays which Avdi Grimm describes in his article Array Set Operations in Ruby 4.

s1 = Set.new([1,2])
s2 = Set.new([2,3])

s1 & s2 # => #<Set: {2}>
s1 | s2 # => #<Set: {1, 2, 3}> 
s1 ^ s2 # => #<Set: {3, 1}>
Conclusion

Next time try a Set!