Method: Array#flatten_once

Defined in:
lib/nuggets/array/flatten_once.rb

#flatten_onceObject

call-seq:

array.flatten_once => new_array

Flatten array by one level only. Pretty straight-forward port of David Alan Black’s flattenx C implementation (though much slower, of course ;-).



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nuggets/array/flatten_once.rb', line 35

def flatten_once
  flat = []

  each { |element|
    if element.is_a?(::Array)
      flat += element
    else
      flat << element
    end
  }

  flat
end