Create a tempfile without opening it in Ruby Create a tempfile without opening it in Ruby ruby ruby

Create a tempfile without opening it in Ruby


You can also use Dir::Tmpname

Dir::Tmpname.create('your_application_prefix') { |path| puts path }

path will contain unique path

See https://github.com/ruby/ruby/blob/ruby_1_9_3/lib/tmpdir.rb#L116


I didn't get an error:

Andrew-Grimms-MacBook-Pro:~ agrimm$ irb>> require "tempfile"=> true>> tempfile = Tempfile.new("temporary_file.txt", "/tmp")=> #<File:/tmp/temporary_file.txt20110622-648-pkynjw-0>>> tempfile.close=> nil>> system("echo foo > #{tempfile.path}")=> true>> system("cat #{tempfile.path}")foo=> true>> tempfile.path=> "/tmp/temporary_file.txt20110622-648-pkynjw-0">> exitAndrew-Grimms-MacBook-Pro:~ agrimm$ cat /tmp/temporary_file.txt20110622-648-pkynjw-0foo

Then again, the temporary file doesn't seem awfully temporary.

Does the error happen with all programs, or just a specific program? Also, can you post the code that causes the problem, and what error backtrace you get?


Is using FileUtils.touch acceptable solution? You can touch a file and delete it once you are done with whatever you want.