Sets and set operations

A set is a hash of keys

A Set is a collection with no duplicates and no order. Under the hood it is a hash whose values are all true, so a lookup costs the same as a hash lookup instead of a scan.

ruby -e 'require "set"; p Set[1, 2, 2, 3].size'

3

set is part of the standard library but not loaded by default; the require at the top of the file is expected. Ruby 3.2 and newer also has Set available without a require in some contexts, but writing it explicitly costs nothing.

Union, intersection, difference

ruby -e 'require "set"; p Set[1, 2] | Set[2, 3]'

#<Set: {1, 2, 3}>

ruby -e 'require "set"; p Set[1, 2] & Set[2, 3]'

#<Set: {2}>

ruby -e 'require "set"; p Set[1, 2, 3] - Set[2]'

#<Set: {1, 3}>

ruby -e 'require "set"; p Set[1].subset?(Set[1, 2])'

true

The four operations cover the questions a program asks about groups: which tags are on both lists, which are only on one, which are on all of them. superset? and intersect? are the other two predicates, and to_a turns the result back into an array for output.

When a set beats an array scan

array.include?(value) walks the array, so a loop over one list that checks another is quadratic. Turn the list that gets checked into a set once and the loop becomes linear:

require "set" allowed = Set.new(%w[admin editor]) users = ["admin", "guest", "editor"] puts users.select { |role| allowed.include?(role) }.inspect

ruby set_demo.rb

["admin", "editor"]

With three items the array would be fine; with thirty thousand the set is the difference between a page that loads and one that times out. The code reads the same either way, which is why the change is easy to make late.

What to watch for

A set uses eql? and hash to decide identity, the same as a hash key. A class with a custom == but no hash keeps two equal objects as two members.

A set promises no order: first answers the first key its hash holds today, which is not something to rely on. Sort the to_a, or keep the array when the order carries meaning.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What are the elements of a set?

    Answer

    Unique values with no promised order

    A set holds each value once, and the order is not part of its contract.

  2. What does Set[1, 2, 2, 3].size return?

    Answer

    3

    The duplicate 2 is stored once, so three members remain.

  3. What does Set[1, 2] | Set[2, 3] return?

    Answer

    #<Set: {1, 2, 3}>

    The union keeps every element of both sets and drops the duplicate.

  4. What does Set[1, 2] & Set[2, 3] return?

    Answer

    #<Set: {2}>

    The intersection keeps only the members both sets hold.

  5. What does Set[1, 2, 3] - Set[2] return?

    Answer

    #<Set: {1, 3}>

    The difference removes every member of the right side.

  6. What does Set[1].subset?(Set[1, 2]) return?

    Answer

    true

    Every member of the small set is a member of the larger one.

  7. Which predicate asks whether two sets share a member?

    Answer

    intersect?

    superset?, subset? and intersect? are the three predicates of the lesson.

  8. Why is a set faster than an array for a membership check in a loop?

    Answer

    The set looks the value up by hash instead of scanning

    A hash lookup is constant time, while include? on an array walks it.

  9. What does this program print? require "set" allowed = Set.new(%w[admin editor]) users = ["admin", "guest", "editor"] puts users.select { |role| allowed.include?(role) }.inspect

    Answer

    ["admin", "editor"]

    select keeps the users the set holds, in the order of the array.

  10. Which methods does a set use to decide whether two values are the same member?

    Answer

    eql? and hash

    The same pair a hash key uses, which is why a custom == without hash keeps two members.

Article author: Arthur Isaev