On this page
Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
Set
set/subclass_compatible.rb - Provides compatibility for set subclasses
Copyright © 2002-2024 Akinori MUSHA [email protected]
Documentation by Akinori MUSHA and Gavin Sinclair.
All rights reserved. You can redistribute and/or modify it under the same terms as Ruby.
An instance of class Set contains a collection of objects (elements), with no duplicates.
By default:
-
Set determines equality via
Object#eql?andObject#hash, and assumes that these values do not change for a stored element. If these values do change, the set enters an unreliable state; seereset. -
A
Stringinstance added to a set is stored as a frozen copy of the string, unless it is already frozen.
Calling compare_by_identity causes:
-
All following determinations of equality to use object identity instead of the methods mentioned above.
-
A
Stringadded to a set is stored “as is”, whether or not frozen.
Set includes module Enumerable, and is easy to use with other enumerable objects. Many of its methods accept enumerable objects as arguments; any enumerable object may be converted to a set via to_set.
Contact
-
Akinori MUSHA <[email protected]> (current maintainer)
Inheriting from Set
Before Ruby 4.0 (released in December, 2025), class Set had a different, less efficient implementation. In Ruby 4.0, the class was reimplemented in C, and the behaviors of some methods were adjusted.
When compatibility with the older implementation is needed, a Set subclass should inherit directly from class Set; this automatically includes module Set::SubclassCompatible, which makes behaviors closer to those in the older implementation.
A difference may be seen as follows:
Set[[1, 2, 3]] # => Set[[1, 2, 3]] class MySet < Set; end MySet[[1, 2, 3]] # => #<MySet: {[1, 2, 3]}> # Same as in Ruby 3.4.
When backward compatibility is not needed, a Set subclass should inherit from Set::CoreSet, which avoids including the compatibility layer:
class MyCoreSet < Set::CoreSet; end MyCoreSet[[1, 2, 3]] # => MyCoreSet[[1, 2, 3]]
What’s Here
First, what’s elsewhere. Class Set:
-
Inherits from class Object.
-
Includes module Enumerable, which provides dozens of additional methods.
In particular, class Set does not have many methods of its own for fetching or for iterating. Instead, it relies on those in Enumerable.
Here, class Set provides methods that are useful for:
Methods for Creating a Set
-
::[]: Returns a new set populated with the given objects. -
::new: Returns a new set based on the given object (if no block given), or on the return values from the called block (if a block given).
Methods for Set Operations
-
&(aliased asintersection): Returns a new set containing the intersection ofselfand the given enumerable. -
-(aliased asdifference): Returns a new set containing the difference ofselfand the given enumerable. -
^: Returns a new set containing the exclusive OR ofselfand the given enumerable. -
|(aliased asunionand+): Returns a new set containing the union ofselfand the given enumerable.
Methods for Comparing
-
<=>: Returns -1, 0, or 1 asselfis less than, equal to, or greater than a given object. -
==: Returns whetherselfand a given enumerable are equal, as determined byObject#eql?. -
compare_by_identity?: Returns whetherselfconsiders only identity when comparing elements. -
proper_subset?(aliased as<): Returns whether the given enumerable is a proper subset ofself. -
proper_superset?(aliased as>): Returns whether the given enumerable is a proper superset ofself. -
subset?(aliased as<=): Returns whether the given object is a subset ofself. -
superset?(aliased as>=): Returns whether the given enumerable is a superset ofself.
Methods for Querying
-
disjoint?: Returns whether no element of the given enumerable is present inself. -
empty?: Returns whetherselfcontains no elements. -
include?(aliased asmember?and===): Returns whether the given object is an element ofself. -
intersect?: Returns whetherselfand the given enumerable have any elements in common. -
size(aliased aslength): Returns the number of elements inself.
Methods for Assigning
-
add(aliased as<<): Adds the given object toself; returnsself. -
add?: Likeadd, but returnsnilif the given object is already inself. -
merge: Adds each element of each of the given enumerables toself; returnsself. -
replace: Replaces the contents ofselfwith the contents of the given enumerable; returnsself.
Methods for Deleting
-
clear: Removes all elements fromself; returnsself. -
delete: Removes the given object fromselfifselfincludes the object; returnsself. -
delete?: Likedelete, but returnsnilif the object is not inself. -
delete_if: Calls the block with each element inself; removes the element if the block returns a truthy value. -
keep_if: Calls the block with each element inself, deleting the element if the block returnsfalseornil; returnsself. -
reject!Likedelete_if, but returnsnilif no changes were made. -
select!(aliased asfilter!): Likekeep_if, but returnsnilif no changes were made. -
subtract: Deletes fromselfevery element found in the given enumerable; returnsself:
Methods for Converting
-
classify: Returns a hash that partitions the elements, as determined by the given block. -
collect!(aliased asmap!): Replaces each element with a block return-value. -
divide: Returns a set of sets that partition the elements, as determined by the given block. -
flatten: Returns a new set that is a recursive flattening ofself. -
flatten!: Likeflatten, but if any changes were made replacesselfwith the result and returnsself. -
inspect(aliased asto_s): Returns a string representation ofself. -
join: Returns the string formed by joining the string-converted elements ofselfwith the given separator. -
to_a: Returns an array containing the elements ofself. -
to_set: With a block given, creates and returns a new set; calls the block with each element ofself, and adds the block’s returns value to the new set.
Other Methods
-
compare_by_identity: Setsselfto compare by object identity (rather than by object content). -
each: Calls the block with each successive element ofself; returnsself. -
reset: Resets the internal state ofself; returnsself. Useful if an element has been modified while an element in the set.
Class methods 2
Instance methods 59
Ruby version changes 5
-
4.1
-
A deprecated behavior,
Set#to_set,Range#to_set, andEnumerable#to_setaccepting arguments, was removed. [Feature #21390]
-
-
4.0
-
Setis now a core class, instead of an autoloaded stdlib class. [Feature #21216] -
Set#inspectnow uses a simpler display, similar to literal arrays. (e.g.,Set[1, 2, 3]instead of#<Set: {1, 2, 3}>). [Feature #21389] -
Passing arguments to
Set#to_setandEnumerable#to_setis now deprecated. [Feature #21390]
-
-
3.2
-
Setis now available as a built-in class without the need forrequire "set". [Feature #16989] It is currently autoloaded via theSetconstant or a call toEnumerable#to_set.
-