title: How do I read a text file into an array of array (each sub-array being a row in the text file?)
body: <p>so I'm pretty much a n00b at Ruby, and I've put together a code to solve a MinCut problem (for an assignment, yes - that part of the code I've put together and tested), and I can't figure out how to read a file and put it into an array of arrays. I have a text file to read, with columns of varying length as below</p>
  
  <blockquote>
    <p>1 37  79  164 </p>
    
    <p>2 123 134 </p>
    
    <p>3        48      123     134   109</p>
  </blockquote>
  
  <p>and I'd like to read it into a 2D array, where each line and columnn is split, with each line going into one array. So the resulting array for the above example would be :</p>
  
  <pre><code>[[1, 37, 79, 164], [2, 123, 134], [3, 48, 123, 134, 109]]
  </code></pre>
  
  <p>My code to read the text file is below:</p>
  
  <pre><code>def read_array(file, count)
    int_array = []
    File.foreach(file) do |f|
      counter = 0
      while (l = f.gets and counter &lt; count ) do
        temp_array = []
        temp_array &lt;&lt; l.to_i.split(" ")
        int_array &lt;&lt; temp_array
        counter = counter + 1
      end
  
    end
    return int_array
  end
  </code></pre>
  
  <p>Any help is greatly appreciated!</p>
  
  <p>Also, if it helps, the error I'm currently getting is "block in read_array': private method 'gets' called for # "</p>
  
  <p>I've tried a few things, and have gotten different error messages though...</p>