title: flat_map in ruby when working with different elements
body: <p>I think flat_map is great when you have an array of hashes of variable lengths and you want to work with each one individually:</p>
  
  <pre><code>data = [
    {'apple' =&gt; 'fruit'},
    {'carrot' =&gt; 'vegetable'},
    {'orange' =&gt; 'fruit', 'pear' =&gt; 'fruit'},
    {'lettuce' =&gt; 'vegetable'}
  ] 
  
  data.flat_map(&amp;:to_a).reduce([]) {|acc, (k,v)| acc &lt;&lt; Hash[k,v] }
   =&gt; [{"apple"=&gt;"fruit"}, {"carrot"=&gt;"vegetable"}, {"orange"=&gt;"fruit"}, {"pear"=&gt;"fruit"}, {"lettuce"=&gt;"vegetable"}] 
  </code></pre>
  
  <p>But I don't think I fully understand flat_map. According to <a href="http://www.apidock.com/ruby/Enumerable/flat_map" rel="nofollow noreferrer">docs</a>:</p>
  
  <blockquote>
    <p>Returns a new array with the concatenated results of running block
    once for every element in enum.</p>
  </blockquote>
  
  <p>But look at this example:</p>
  
  <pre><code>[[1], {a: "a"}, {b: "b"}].flat_map(&amp;:to_a)
  =&gt; [1, [:a, "a"], [:b, "b"]] 
  </code></pre>
  
  <p>The first item is stripped from the inner array, the other items are converted to arrays. How does it know to do this?</p>
  
  <p>When I call to_a like this:</p>
  
  <pre><code>[1].to_a
   =&gt; [1] 
  [1].to_a.flatten
   =&gt; [1] 
  </code></pre>
  
  <p>You see the result is different than what flat_map did. What is flat_map doing in the first example to strip the item of the array?</p>