Will CSV.open
store data in memory and write to file one time when the block exits, or it will automatically write in many batches?
require 'csv'
CSV.open('result.csv', 'wb') do |csv|
while row = next_row
csv << row
end
end
CSV (comma-separated variables) data is a text representation of a table: A row separator delimits table rows. A common row separator is the newline character "\n" . A column separator delimits fields in a row. A common column separator is the comma character "," .
CSV.open
will write to the underlying OS when the block closes, and it will also write every time the buffer fills and is flushed, which will happen automatically. (In my Ruby install, it happens at 8196 bytes.) You can also add csv.flush
to your block to force it to write sequentially.
require 'csv'
CSV.open('result.csv', 'wb') do |csv|
while row = next_row
csv << row # Without flush, writes to underlying OS only when buffer fills
csv.flush # Adding this forces a write to underlying OS
end
end # Exiting block writes to underlying OS and closes file handle
(Note that the writes are to the underlying OS, which may buffer the stream again before actually writing to disk.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With