Module: Enumerable

Defined in:
lib/enumerable/merge.rb

Instance Method Summary collapse

Instance Method Details

#merge_left(other_enum, id_method = nil) ⇒ Object

The left enumerator is accessed first



21
22
23
# File 'lib/enumerable/merge.rb', line 21

def merge_left(other_enum, id_method = nil)
  other_enum.merge_right(self, id_method)
end

#merge_right(other_enum, id_method = nil) ⇒ Object

The right enumerator is accessed first



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/enumerable/merge.rb', line 3

def merge_right(other_enum, id_method = nil)
  Enumerator.new do |yielder|
    yielded_ids = []

    other_enum.each do |x|
      id = id_method ? x.send(id_method) : x
      yielded_ids << id
      yielder << x
    end

    self.each do |x|
      id = id_method ? x.send(id_method) : x
      yielder << x unless yielded_ids.include? id
    end
  end.lazy
end