9.5. Sets

Set is the one class under discussion in this chapter that isn’t, strictly speaking, a Ruby core class. It’s a standard library class, which means that to use it, you have to do this:

require 'set'

The general rule in this book is that we’re looking at the core language rather than the standard library, but the Set class makes a worthy exception because it fits in so nicely with the other container and collection classes we’ve looked at.

A set is a unique collection of objects. The objects can be anything—strings, integers, arrays, other sets—but no object can occur more than once in the set. Uniqueness is also enforced at the commonsense content level: if the set contains the string "New York", you can’t add the string "New York"

Get The Well-Grounded Rubyist, Second Edition 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.