Does the Ruby CSV.open buffer load into memory and write all at once?

Will it CSV.openstore data in memory and write to the file once when the block comes out, or will it be automatically written in many batches?

require 'csv'

CSV.open('result.csv', 'wb') do |csv|
  while row = next_row
    csv << row
  end
end
+4
source share
1 answer

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  # Writes to underlying OS only when buffer fills
    csv.flush   # Forces write to underlying OS
  end
end             # Writes to underlying OS and closes file handle

( , , .)

+5

Source: https://habr.com/ru/post/1619130/


All Articles