Create a tempfile without opening it in Ruby

Viewed 4625

Is there a way to create a tempfile, without having it opened? I have to run an executable, redirect it's output to a file, and then read & parse that. Everything created by tempfile is already opened, and this triggers an error , because the file is locked.

5 Answers

The @timfjord's answer works. But if you don't need a block try:

Dir::Tmpname.create(['prefix-', '.ext']) {}
# => "/tmp/prefix-20190827-1-87n9iu.ext"
Related