Home
Ruby
Set Examples
Updated Oct 13, 2021
Dot Net Perls
Set. In Ruby, sets have only unique elements. Like a hash, a set provides fast lookups. But it stores no values alongside the keys—it only stores keys.
Logical methods. Sets provide many logical methods for adding, removing and testing elements. We can use operations like subset for easy analysis.
Hash
First example. This example creates a new Set with the Set.new syntax. The argument to new() is an array of 2 elements—each element is added to the set.
Start We call the add() method twice. One string, "socrates," is added but already exists. The duplicate is not stored.
Next We call the size() method on the set, which returns 3. The set has 3 elements because the duplicate was not retained.
Finally We invoke the "include" method. This returns true because the argument exists within the set.
Info When we call "include," a hashed lookup occurs. In large sets, this is much faster than a linear search through the elements.
Array
require "set" # Create new Set. s = Set.new(["plato", "socrates"]) # Add two more elements. s.add("cebes") s.add("socrates") # Get size. puts s.size() # See if Set includes this element. puts s.include?("socrates")
3 true
Merge, each. Next, this program uses the merge() method and the each() iterator. With merge(), we pass in a collection and the set adds all of its elements.
Detail With each, we iterate over the elements in the set. This uses the same syntax forms available on the hash and array in Ruby.
Iterator
Tip This is the simplest way to loop over a set. In this example, we use the identifier "n."
require "set" # Create set. s = Set.new(["x", "y"]) # Merge in these two elements. s.merge(["y", "z"]) # Iterate the set. s.each do |n| # Display the element. puts n end
x y z
Subset, operators. One common operation with a set is the "subset" test. We can determine if all elements from one set are contained within another.
require "set" # Contains three elements. set1 = Set.new [10, 20, 30] # Is a superset of set1. set2 = Set.new [10, 20, 30, 40, 50] # This is true. if set1 <= set2 puts true end # This is not evaluated to true. if set2 <= set1 puts false end # This is true. # ... Subset is the same as the <= operator. if set1.subset?(set2) puts true end
true true
A summary. A set stores just keys, not values. In this way, it is like a hash with no values. In places where no values are required, this leads to clearer code.
Set benefits. Performance with sets is good—hashed lookups make searches faster. But the greater benefit with a set is its logical methods, which simplify code.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Oct 13, 2021 (image).
Home
Changes
© 2007-2025 Sam Allen