title: Inserting values at empty indexes in arrays. How can I write this code shorter or more modular?
body: <p>I have the following code I wrote which takes a 2d array with arrays of varying length and appends the value 0 to every index until all arrays are of the same length.</p>
  
  <p>Can this code be written shorter or more efficient and modular? </p>
  
  <pre><code>a = [[1,7],[2,3],[5,1,2],[3],[1],[]]
  
  l = a.map(&amp;:length).max 
  a2 = a.each{|e| e.push(Array.new(l - e.length, 0) )}
  a2.each{|e| e.flatten!}
  
  #=&gt; [[1, 7, 0], [2, 3, 0], [5, 1, 2], [3, 0, 0], [1, 0, 0], [0, 0, 0]]
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Because I feel unable to award an accepted answer to the two answers given thus far based on the simplicity of the written code itself, I have decided to award it based on the efficiency of the running speed of the code.</p>
  
  <pre><code>100 test cases running code block 10000 times.
  
  #My Code:
  #average run time of test case -&gt; 0.24267sec
  #standard deviation -&gt; 0.00735sec
  
  #Stefan’s Code:
  #average run time of test case -&gt; 0.06389sec
  #standard deviation -&gt; 0.00756sec
  
  #steenslag’s Code:
  #average run time of test case -&gt; 0.0577sec
  #standard deviation -&gt; 0.00413sec
  </code></pre>
  
  <p>tests were conducted under the same conditions using a custom written ruby timer class I made and are only relative to each other and my crappy 2010-macbookpro on which I ran them.</p>