Set
implements a collection of unordered values with no duplicates.
An Enumerable
object can be converted to Set
using the #to_set
method.
Set
uses Hash
as storage, so you must note the following points:
Object#==
and Object#hash
.Set
assumes that the identity of each element does not change while it is stored. Modifying an element of a set will render the set to an unreliable state.s1 = Set{1, 2} s2 = [1, 2].to_set s3 = Set.new [1, 2] s1 == s2 # => true s1 == s3 # => true s1.add(2) s1.concat([6, 8]) s1.subset? s2 # => false s2.subset? s1 # => true
Creates a new set from the elements in enumerable.
Creates a new, empty Set
.
Intersection: returns a new set containing elements common to both sets.
Addition: returns a new set containing the unique elements from both sets.
Difference: returns a new set containing elements in this set that are not present in the other enumerable.
Difference: returns a new set containing elements in this set that are not present in the other.
Alias for #add
Returns true
if both sets have the same elements.
Same as #includes?
.
Symmetric Difference: returns a new set (self - other) | (other - self)
.
Symmetric Difference: returns a new set (self - other) | (other - self)
.
Adds object to the set and returns self
.
Adds object to the set and returns true
on success and false
if the value was already in the set.
Removes all elements in the set, and returns self
.
Returns a new Set
with all of the elements cloned.
Returns true
of this Set is comparing objects by object_id
.
Adds #each
element of elems to the set and returns self
.
Removes the object from the set and returns self
.
Returns a new Set
with all of the same elements.
Returns an iterator for each element of the set.
Yields each element of the set, and returns self
.
Returns true
if the set is empty.
Returns true
if object exists in the set.
Alias of #to_s
.
Returns true
if the set and the given set have at least one element in common.
Returns true
if the set is a proper subset of the other set.
Returns true
if the set is a superset of the other set.
Returns the number of elements in the set.
Returns true
if the set is a subset of the other set.
Returns self
after removing from it those elements that are present in the given enumerable.
Returns true
if the set is a superset of the other set.
Returns the elements as an Array
.
Writes a string representation of the set to io.
Union: returns a new set containing all unique elements from both sets.
Iterable(T)
Enumerable(T)
Struct
Value
Object
Object
Creates a new set from the elements in enumerable.
a = [1, 3, 5] s = Set.new a s.empty? # => false
Intersection: returns a new set containing elements common to both sets.
Set{1, 1, 3, 5} & Set{1, 2, 3} # => Set{1, 3} Set{'a', 'b', 'b', 'z'} & Set{'a', 'b', 'c'} # => Set{'a', 'b'}
Addition: returns a new set containing the unique elements from both sets.
Set{1, 1, 2, 3} + Set{3, 4, 5} # => Set{1, 2, 3, 4, 5}
Difference: returns a new set containing elements in this set that are not present in the other enumerable.
Set{1, 2, 3, 4, 5} - [2, 4] # => Set{1, 3, 5} Set{'a', 'b', 'b', 'z'} - ['a', 'b', 'c'] # => Set{'z'}
Difference: returns a new set containing elements in this set that are not present in the other.
Set{1, 2, 3, 4, 5} - Set{2, 4} # => Set{1, 3, 5} Set{'a', 'b', 'b', 'z'} - Set{'a', 'b', 'c'} # => Set{'z'}
Returns true
if both sets have the same elements.
Set{1, 5} == Set{1, 5} # => true
Same as #includes?
.
It is for convenience with using on case
statement.
red_like = Set{"red", "pink", "violet"} blue_like = Set{"blue", "azure", "violet"} case "violet" when red_like & blue_like puts "red & blue like color!" when red_like puts "red like color!" when blue_like puts "blue like color!" end
See also: Object#===
.
Symmetric Difference: returns a new set (self - other) | (other - self)
. Equivalently, returns (self | other) - (self & other)
.
Set{1, 2, 3, 4, 5} ^ Set{2, 4, 6} # => Set{1, 3, 5, 6} Set{'a', 'b', 'b', 'z'} ^ Set{'a', 'b', 'c'} # => Set{'z', 'c'}
Symmetric Difference: returns a new set (self - other) | (other - self)
. Equivalently, returns (self | other) - (self & other)
.
Set{1, 2, 3, 4, 5} ^ [2, 4, 6] # => Set{1, 3, 5, 6} Set{'a', 'b', 'b', 'z'} ^ ['a', 'b', 'c'] # => Set{'z', 'c'}
Adds object to the set and returns self
.
s = Set{1, 5} s.includes? 8 # => false s << 8 s.includes? 8 # => true
Adds object to the set and returns true
on success and false
if the value was already in the set.
s = Set{1, 5} s.add? 8 # => true s.add? 8 # => false
Removes all elements in the set, and returns self
.
s = Set{1, 5} s.size # => 2 s.clear s.size # => 0
Makes this set compare objects using their object identity (object_id)
for types that define such method (Reference
types, but also structs that might wrap other Reference
types and delegate the object_id
method to them).
s = Set{"foo", "bar"} s.includes?("fo" + "o") # => true s.compare_by_identity s.compare_by_identity? # => true s.includes?("fo" + "o") # => false # not the same String instance
Returns true
of this Set is comparing objects by object_id
.
See #compare_by_identity
.
Removes the object from the set and returns self
.
s = Set{1, 5} s.includes? 5 # => true s.delete 5 s.includes? 5 # => false
Returns an iterator for each element of the set.
Yields each element of the set, and returns self
.
Returns true
if the set is empty.
s = Set(Int32).new s.empty? # => true s << 3 s.empty? # => false
Returns true
if object exists in the set.
s = Set{1, 5} s.includes? 5 # => true s.includes? 9 # => false
Returns true
if the set and the given set have at least one element in common.
Set{1, 2, 3}.intersects? Set{4, 5} # => false Set{1, 2, 3}.intersects? Set{3, 4} # => true
Returns true
if the set is a proper subset of the other set.
This set must have fewer elements than the other set, and all of elements in this set must be present in the other set.
Set{1, 5}.proper_subset? Set{1, 3, 5} # => true Set{1, 3, 5}.proper_subset? Set{1, 3, 5} # => false
Returns true
if the set is a superset of the other set.
The other must have the same or fewer elements than this set, and all of elements in the other set must be present in this set.
Set{1, 3, 5}.proper_superset? Set{1, 5} # => true Set{1, 3, 5}.proper_superset? Set{1, 3, 5} # => false
Returns the number of elements in the set.
s = Set{1, 5} s.size # => 2
Returns true
if the set is a subset of the other set.
This set must have the same or fewer elements than the other set, and all of elements in this set must be present in the other set.
Set{1, 5}.subset? Set{1, 3, 5} # => true Set{1, 3, 5}.subset? Set{1, 3, 5} # => true
Returns self
after removing from it those elements that are present in the given enumerable.
Set{'a', 'b', 'b', 'z'}.subtract Set{'a', 'b', 'c'} # => Set{'z'} Set{1, 2, 3, 4, 5}.subtract [2, 4, 6] # => Set{1, 3, 5}
Returns true
if the set is a superset of the other set.
The other must have the same or fewer elements than this set, and all of elements in the other set must be present in this set.
Set{1, 3, 5}.superset? Set{1, 5} # => true Set{1, 3, 5}.superset? Set{1, 3, 5} # => true
Union: returns a new set containing all unique elements from both sets.
Set{1, 1, 3, 5} | Set{1, 2, 3} # => Set{1, 3, 5, 2} Set{'a', 'b', 'b', 'z'} | Set{'a', 'b', 'c'} # => Set{'a', 'b', 'z', 'c'}
See also: #concat
to add elements from a set to self
.
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/Set.html