require 'set' s = [1, 2, 3, -1, -2, -4].to_set # Divide the set into sets of numbers with the same absolute value. p s.divide { |x,y| x.abs == y.abs } # => #<Set: {#<Set: {-1, 1}>, # => #<Set: {2, -2}>, # => #<Set: {-4}>, # => #<Set: {3}>}>