title: Most Efficient way to Recursively Flatten Nested Array to Dot-separated string in Ruby?
body: <p>I want to convert something like this:</p>
  
  <pre><code>class NestedItem
    attr_accessor :key, :children
    def initialize(key, &amp;block)
      self.key = key
      self.children = []
      self.instance_eval(&amp;block) if block_given?
    end
  
    def keys
      [key] + children.keys
    end
  end
  
  root = NestedItem.new("root") do
    children &lt;&lt; NestedItem.new("parent_a") do
      children &lt;&lt; NestedItem.new("child_a")
      children &lt;&lt; NestedItem.new("child_c")
    end
    children &lt;&lt; NestedItem.new("parent_b") do
      children &lt;&lt; NestedItem.new("child_y")
      children &lt;&lt; NestedItem.new("child_z")
    end
  end
  
  require 'pp'
  pp root
  #=&gt;
  # #&lt;NestedItem:0x1298a0
  #  @children=
  #   [#&lt;NestedItem:0x129814
  #     @children=
  #      [#&lt;NestedItem:0x129788 @children=[], @key="child_a"&gt;,
  #       #&lt;NestedItem:0x12974c @children=[], @key="child_c"&gt;],
  #     @key="parent_a"&gt;,
  #    #&lt;NestedItem:0x129738
  #     @children=
  #      [#&lt;NestedItem:0x129698 @children=[], @key="child_y"&gt;,
  #       #&lt;NestedItem:0x12965c @children=[], @key="child_z"&gt;],
  #     @key="parent_b"&gt;],
  #  @key="root"&gt;
  </code></pre>
  
  <p>Into this:</p>
  
  <pre><code>root.keys #=&gt;
  [
    "root",
    "root.parent_a",
    "root.parent_a.child_a",
    "root.parent_a.child_c",
    "root.parent_b",
    "root.parent_b.child_y",
    "root.parent_b.child_z",
  ]
  </code></pre>
  
  <p>...using a recursive method.</p>
  
  <p>What's the simplest way to go about this?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I did this:</p>
  
  <pre><code>def keys
    [key] + children.map(&amp;:keys).flatten.map do |node|
      "#{key}.#{node}"
    end
  end
  </code></pre>
  
  <p>Anything better?</p>