If I've created a temporary file through Tempfile
, is there any way aside from copying it to another file that I can make it "permanent"? I'd like to avoid it getting deleted when the associated Tempfile
instance is garbage collected or the process is terminated.
On a related point, is there a way to leverage the Tempfile
mechanism (or use a similar mechanism) to get a "new" filename without having to create a file at that name?
Not really. For the question itself, see this:
ObjectSpace.undefine_finalizer(tmpfile)
The Tempfile library uses Ruby ObjectSpace finalizers to automatically delete itself on garbage collection. By using the above line you can remove the Tempfile's ability to delete itself if you don't delete it. So, for example:
$ irb
2.0.0p0 :001 > require "tempfile"
=> true
2.0.0p0 :002 > t = Tempfile.new("test")
=> #<Tempfile:/tmp/test20140122-6655-80p4b7>
2.0.0p0 :003 > t.write("Hi!")
=> 3
2.0.0p0 :004 > ObjectSpace.undefine_finalizer(t)
=> #<Tempfile:/tmp/test20140122-6655-80p4b7>
2.0.0p0 :005 > exit
$ cat /tmp/test20140122-6655-80p4b7
Hi!
$
There's something else to be aware of though. Tempfile will use system temporary file directories like /tmp
that the OS automatically cleans out every once in a while (for example on every boot). Because of this, even if you "persist" the file, you either need to be OK with it disappearing, or move it to a directory that doesn't get cleaned out by default, like /var/tmp
(the Linux directory for persistant temporary files).
As for your second question, try this code from here:
Dir::Tmpname.create('your_application_prefix') { |path| puts path }
It requires a require "tmpdir"
.