Ruby - convert text file to array

I am very new to Ruby on Rails and use it to increase C ++ code. My C ++ code is currently outputting data from a multidimensional array to a text file, such as:

2 2 2 2 2 3 1 1 1 1 5 2 2 2 2 2 2 2 2 3 1 1 1 1 1 1 1 1 5 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 3 1 1 1 1 1 1 1 1 1 1 1 1 5 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 6 1 1 1 1 1 1 1 1 1 1 1 1 4 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 6 1 1 1 1 1 1 1 1 4 2 2 2 2 2 2 2 2 6 1 1 1 1 4 2 2 2 2 2 

I am looking for help in converting this text output to a two-dimensional array for Ruby input with dynamic height / width. So far, I have entered them manually in my Ruby code, but will soon do a lot more tests, and so far I have not been able to find a way to convert it into a two-dimensional Ruby array. Any help would be great!

+7
source share
2 answers
 File.foreach('file.txt').map { |line| line.split(' ') } 
+13
source
 File.readlines('foo.txt').map &:split 
+9
source

All Articles