Reading numbers into an array in ruby

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.

+3
3

- .

f = File.read 'names.txt' 
f.split(';').each  do |set|
  method_x(set.split.map(&:to_i))
end

, method_x arg.

+2

:

File.readlines('a.txt',';').each do |string|
  ary = string.scan(/\d+/).map(&:to_i)
  method_x(ary)
end

;, readlines(name, sep=$/ [, open_args]).

, , . sep.

+3
File.read('names.txt').split(';').each { |x| the_function(x.split.map(&:to_i)) }

where the_functionis your desired function

0
source

All Articles