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
1 37 79 164
2 123 134
3 48 123 134 109
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 :
[[1, 37, 79, 164], [2, 123, 134], [3, 48, 123, 134, 109]]
My code to read the text file is below:
def read_array(file, count)
int_array = []
File.foreach(file) do |f|
counter = 0
while (l = f.gets and counter < count ) do
temp_array = []
temp_array << l.to_i.split(" ")
int_array << temp_array
counter = counter + 1
end
end
return int_array
end
Any help is greatly appreciated!
Also, if it helps, the error I'm currently getting is "block in read_array': private method 'gets' called for # "
I've tried a few things, and have gotten different error messages though...