Ruby - 在内存中创建文件 [英] Ruby - Creating a file in memory

查看:223
本文介绍了Ruby - 在内存中创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在Ruby中编写下面的代码而不将文件写入磁盘?

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

temp_file = 'path/to/file.csv'
users = [a@b.c, c@b.a]

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

Reports.sendreport users temp_file

File.delete(temp_file)

Reports.sendreport附加文件并发送电子邮件,因此它需要是一个文件...

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

推荐答案

尝试其中一个mmap宝石。如果只有只有 需要一个文件名,这是你的选择。

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

但是,如果它可以接受类似文件的对象, StringIO。

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

您可以考虑更改 Reports 的任何一个,使其更通用。这取决于它用来创建邮件消息–这可能是微不足道的。

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.

这篇关于Ruby - 在内存中创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆