Ruby - Hash Sets


The Set is a collection of unordered values with no duplicates.

You can initialize a Set with an array of values, in which case duplicates are ignored:

s1 = [1,2,3,4,5,2] ) 
s2 = [1,1,2,3,4,4,5,1] ) 
s3 = [1,2,100] ) 
weekdays = %w( Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday ) ) 

You can add new values using the add method:

s1.add( 1000 ) 

The merge method combines values of one set with another:


You can use == to test for equality.

Two sets that contain the same values are considered to be equal:

p( s1 == s2 )            #=> true 

To sort a set, convert a set to an array using the to_a method and use a standard or custom sort:

p( weekdays.to_a.sort ) # sort alphabetically