Class: Array

Inherits:
Object show all
Defined in:
lib/ruby_us/extensions/array.rb

Instance Method Summary collapse

Instance Method Details

#intersection(set) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# 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) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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