LANGUAGE » RUBY

Set

Basic

Initializing a Set:

rb
myset = Set.new
myset.add(value)

Create a Set from an Array:

rb
myset = myarray.to_set

Operators

OperatorDescription
+Union
-Difference
&Intersection
^Symmetric difference

Methods

MethodDescription
to_aSetArray
sizeNumber of elements.
empty?true if there are no elements.
include?Returns true if the set includes the specified element.
mapReturns a new set with each element updated by the given block.
collectSame as map.
filterReturns a new set with only elements evaluated as true by the given block.

Check if set includes an element:

ruby
myset.include? 'value'