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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access