Set
A Ruby Set is somewhere between an Array and a Hash—it’s a collection of unique items. The Set class is a subclass of Object, and it defines each and includes Enumerable, so all Enumerable methods described in this chapter apply to sets. The elements of a Set are ordered the way that Hash keys are ordered—they preserve the sequence in which the elements were added to the Set. In other words, iterating over a set multiple times will always result in the same ordering, but you can’t access arbitrary elements of the set via an index.
To use Set in versions of Ruby before 3.2, you need to explicitly call require "set".
Creating Sets
There are several ways to create a Set. The Enumerable class defines Enumerable#to_set, which converts the collection ...
Get Programming Ruby 3.3 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.