Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/achoo/extensions.rb

Instance Method Summary collapse

Instance Method Details

#merge!(other) ⇒ Object

Useful for merging two sorted arrays with Comparable elements.

The content of the two input arrays should not be trusted after being mistreated by this method.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/achoo/extensions.rb', line 14

def merge!(other)
  # FIX raise exception if merge is defined?
  array = []
  until empty? or other.empty?
    if first <= other.first
      array << shift
    else
      array << other.shift
    end
  end
  array.concat(self).concat(other)
  array
end