flatten_array.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Flatten a nested array into a single array with all values except nil
class FlattenArray
  VERSION = 1

  def self.flatten(array)
    return [array] unless array.is_a? Array
    return [] if array.empty?
    first, *rest = *array
    if first.nil?
      flatten(rest)
    else
      flatten(first) + flatten(rest)
    end
  end
end

Comments


You're not logged in right now. Please login via GitHub to comment