module Enumerable

Public Instance Methods

flatten_with_path(parent_prefix = '$') click to toggle source
# File bin/flatten, line 27
def flatten_with_path(parent_prefix = '$')
  res = {}

  self.each_with_index do |elem, i|
    if elem.is_a?(Array)
      k, v = elem
    else
      k, v = i, elem
    end

    key = parent_prefix ? "#{parent_prefix}.#{k}" : k # assign key name for result hash

    if v.is_a? Enumerable
      res.merge!(v.flatten_with_path(key)) # recursive call to flatten child elements
    else
      res[key] = v
    end
  end

  res
end