Add set operation to Array
require 'set'
class Array
def classify
h = {}
each do |i|
x = yield(i)
(h[x] ||= self.class.new) << i
end
h
end
def divide(&block)
Set.new(classify(&block).values)
end
end
p [1,1,2,6,6,7,101].divide { |x| x % 2 }
# => #<Set: {[2, 6, 6], [1, 1, 7, 101]}>
Related examples in the same category