class Array

Public Instance Methods

intersection(set) click to toggle source
# File lib/ruby_us/extensions/array.rb, line 3
def intersection set
  all = [self, set]

  smaller = (all = all.sort do |first, second|
    first.count <=> second.count
  end).first

  larger = all[1]

  smaller.select do |item|
    larger.include? item
  end.uniq
end
intersections(*sets) click to toggle source
# File lib/ruby_us/extensions/array.rb, line 17
def intersections *sets
  all = sets.push(self)

  smaller = all.sort do |first, second|
    first.count <=> second.count
  end.first

  all.delete smaller

  intersections = smaller.intersection(all.first)

  sets.reduce do |list|
    intersections = intersections.intersection list
  end
end