title: What is the most efficient way to collect arrays of nested hash keys that access the leaf-nodes?
body: <p>I have a nested hash and would like a single array, where each element is an array of keys which represents a path through a nested hash to the non-hashes (leaf-nodes).</p>
  
  <p>For example, given the input:</p>
  
  <pre><code>x = Hash.new
  x["a"] = Hash.new
  x["a"]["b"] = Hash.new
  x["a"]["b"]["c"] = "one"
  x["a"]["b"]["d"] = "two"
  x["a"]["e"] = "three"
  x["f"] = Hash.new
  x["f"]["g"] = "four"
  </code></pre>
  
  <p>I would like the output:</p>
  
  <pre><code>[["a", "b", "c"], ["a", "b", "d"], ["a", "e"], ["f", "g"]]
  </code></pre>
  
  <p>The code below works using two recursive methods: one to generate a nested array, and the other to un-nest it !.</p>
  
  <p>My Ruby intuition tells me there must be a much more efficient and elegant way of achieving this.  Can anyone suggest a 'golf' solution, or a perfectly Ruby Way solution?</p>
  
  <pre><code>def collect_key_paths(object, path=[])
    result = nil
    if object.is_a?(Hash)
      path_for_current_hash = object.map do |key, value|
        incremented_path = [path, key].flatten
        collect_key_paths(value, incremented_path)
      end
      result = path_for_current_hash
    else
      result = path
    end
    result
  end
  
  def smoothe(array, store=[])
    if array.none? { |element| element.is_a?(Array) }
      store &lt;&lt; array
    else
      array.each do |element|
        store = smoothe(element, store)
      end
    end
    store
  end
  
  
  x = Hash.new
  x["a"] = Hash.new
  x["a"]["b"] = Hash.new
  x["a"]["b"]["c"] = "one"
  x["a"]["b"]["d"] = "two"
  x["a"]["e"] = "three"
  x["f"] = Hash.new
  x["f"]["g"] = "four"
  
  nested_key_paths = collect_key_paths(x)
  puts "RESULT:#{smoothe(nested_key_paths)}"
  </code></pre>
  
  <p>The result of this code, running version 1.9.2 is:</p>
  
  <pre><code>RESULT:[["a", "b", "c"], ["a", "b", "d"], ["a", "e"], ["f", "g"]]
  </code></pre>
  
  <p>Thank you!</p>