Class

Set

Extends:

Includes:

Set implements a collection of unordered values with no duplicates. This is a hybrid of Array’s intuitive inter-operation facilities and Hash’s fast lookup.

Several methods accept any Enumerable object (implementing each) for greater flexibility: new, replace, merge, subtract, |, &, -, ^.

The equality of each couple of elements is determined according to Object#eql? and Object#hash, since Set uses Hash as storage.

Finally, if you are using class Set, you can also use Enumerable#to_set for convenience.

Example

require 'set'
s1 = Set.new [1, 2]                   # -> #<Set: {1, 2}>
s2 = [1, 2].to_set                    # -> #<Set: {1, 2}>
s1 == s2                              # -> true
s1.add("foo")                         # -> #<Set: {1, 2, "foo"}>
s1.merge([2, 6])                      # -> #<Set: {6, 1, 2, "foo"}>
s1.subset? s2                         # -> false
s2.subset? s1                         # -> true
Constants
InspectKey
Public Methods
| Returns a new set built by merging the set and the elements of the given enumerable object.
& Returns a new set containing elements common to the set and the given enumerable object.
+ Alias for #|
- Returns a new set built by duplicating the set, removing every element that appears in the given enumerable object.
<< Alias for #add
== Returns true if two sets are equal. The equality of each couple of elements is defined according to Object#eql?.
[] Creates a new set containing the given objects.
^ Returns a new set containing elements exclusive between the set and the given enumerable object. (set ^ enum) is equivalent to ((set | enum) - (set & enum)).
add Adds the given object to the set and returns self. Use merge to add several elements at once.
add? Adds the given object to the set and returns self. If the object is already in the set, returns nil.
classify Classifies the set by the return value of the given block and returns a hash of {value => set of elements} pairs. The block is called once for each element of the set, passing the element as parameter.
clear Removes all elements and returns self.
collect! Do collect() destructively.
delete Deletes the given object from the set and returns self. Use subtract to delete several items at once.
delete? Deletes the given object from the set and returns self. If the object is not in the set, returns nil.
delete_if Deletes every element of the set for which block evaluates to true, and returns self.
difference Alias for #-
divide Divides the set into a set of subsets according to the commonality defined by the given block.
each Calls the given block once for each element in the set, passing the element as parameter.
empty? Returns true if the set contains no elements.
eql?
flatten Returns a new set that is a copy of the set, flattening each containing set recursively.
flatten! Equivalent to Set#flatten, but replaces the receiver with the result in place. Returns nil if no modifications were made.
hash
include? Returns true if the set contains the given object.
initialize_copy Copy internal hash.
inspect Returns a string containing a human-readable representation of the set. ("#<Set: {element1, element2, …}>")
intersection Alias for #&
length Alias for #size
map! Alias for #collect!
member? Alias for #include?
merge Merges the elements of the given enumerable object to the set and returns self.
new Creates a new set containing the elements of the given enumerable object.
pretty_print
pretty_print_cycle
proper_subset? Returns true if the set is a proper subset of the given set.
proper_superset? Returns true if the set is a proper superset of the given set.
reject! Equivalent to Set#delete_if, but returns nil if no changes were made.
replace Replaces the contents of the set with the contents of the given enumerable object and returns self.
size Returns the number of elements.
subset? Returns true if the set is a subset of the given set.
subtract Deletes every element that appears in the given enumerable object and returns self.
superset? Returns true if the set is a superset of the given set.
to_a Converts the set to an array. The order of elements is uncertain.
union Alias for #|
Protected Methods
flatten_merge
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.