I am trying to read numbers from a txt file into an array in ruby with some conditions. Entering a text file is as follows:
1 2 3 4
5 6 7 8
9; 10 11 12
What I want to do is read the numbers before ';', store them in an array, execute a method in that array, and then clear the array and start reading from the semicolon. Here you can see the program execution using the input presented above:
read [1,2,3,4,5,6,7,8,9] → Execute method X → [] Clear array → [10 11 12] → execute method X ...
I think a variant of the following code could do the trick, but I don’t know enough rubies to do it myself.
a = []
File.open('names.txt') do |f|
f.each_line do |line|
a << line.split.map(&:to_i)
end
end
Thank you for your help! This is curiosity, not an assignment or anything else. I'm trying to create ruby skills by doing simple things.