Ruby - Creating a file in memory

You could use Tempfile. Tempfile writes the file to disc, so it does not fit your request.

But I think Tempfile provides some features you need:

When a Tempfile object is garbage collected, or when the Ruby interpreter exits, its associated temporary file is automatically deleted.

Example:

require 'tempfile'
require 'csv'

data_for_report = [1,2,3,4]
temp_file = Tempfile.new('foo')

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

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.

Tags:

Csv

Ruby

Io