Mega Code Archive

 
Categories / Ruby / Collections
 

Add element to a set

require 'set' s = Set[]              # start with an empty set s << 1                 # => #<Set: {1}> s.add 2                # => #<Set: {1, 2}>: add is a synonym for << s << 3 << 4 << 5       # => #<Set: {5, 1, 2, 3, 4}>: can be chained s.add 3                # => #<Set: {5, 1, 2, 3, 4}>: value unchanged s.add? 6               # => #<Set: {5, 6, 1, 2, 3, 4}> s.add? 3               # => nil: the set was not changed