Ruby: Is there a better way to iterate over multiple (big) files?

Posted by zxcvbnm on Stack Overflow See other posts from Stack Overflow or by zxcvbnm
Published on 2010-04-15T00:52:03Z Indexed on 2010/04/15 1:03 UTC
Read the original article Hit count: 367

Filed under:
|
|
|

Here's what I'm doing (sorry for the variable names, I'm not using those in my code):

File.open("out_file_1.txt", "w") do |out_1|
  File.open("out_file_2.txt", "w") do |out_2|
    File.open_and_process("in_file_1.txt", "r") do |in_1|
      File.open_and_process("in_file_2.txt", "r") do |in_2|
        while line_1 = in_1.gets do
          line_2 = in_2.gets        #input files have the same number of lines
          #process data and output to files
        end
      end
    end
  end
end

The open_and_process method is just to open the file and close it once it's done. It's taken from the pickaxe book.

Anyway, the main problem is that the code is nested too deeply. I can't load all the files' contents into memory, so I have to iterate line by line. Is there a better way to do this? Or at least prettify it?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about refactoring