Search code examples
rubycsvio

Ruby - Creating a file in memory


Is there anyway to write the following code in Ruby without writing the file to disk?

temp_file = 'path/to/file.csv'
users = [[email protected], [email protected]]

CSV.open(temp_file, "w") do |csv|
  csv << data_for_report
end

Reports.sendreport users temp_file

File.delete(temp_file)

The Reports.sendreport attaches a file and sends an email, so it needs to be a file...


Solution

  • Try one of the mmap gems. If the library only takes a filename, that's your option.

    If it can accept a file-like object, however, you can use a StringIO.

    You might consider changing whatever Reports is, making it more general-purpose. It depends on what it's using to create its mail message–this might be trivial.