CSV.openwill be written to the base OS when the block is closed, and it will also write every time the buffer is filled and flushed, which will happen automatically. (In my Ruby installation, this happens with 8196 bytes.) You can also add csv.flushto your block to make it write sequentially.
require 'csv'
CSV.open('result.csv', 'wb') do |csv|
while row = next_row
csv << row
csv.flush
end
end
( , , .)