Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a tempfile without opening it in Ruby

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.

like image 585
Geo Avatar asked Jun 22 '11 09:06

Geo


People also ask

How do you create a temp file in Ruby?

In any case, all arguments ( basename , tmpdir , mode , and **options ) will be treated as ::new. Creates a temporary file with permissions 0600 (= only readable and writable by the owner) and opens it with mode “w+”. The temporary file will be placed in the directory as specified by the tmpdir parameter.

What is file rewind in Ruby?

Rewind resets the line number to zero f = File.new("testfile") f.readline #=> "This is line one\n" f.rewind #=> 0 f.lineno #=> 0 f.readline #=> "This is line one\n" IO#close. Closes ios and flushes any pending writes to the operating system. read([length [, outbuf]]) Reads length bytes from the I/O stream.

What is a tmp file extension?

A TMP file refers to a transitory backup, storage, or other file system generated by a software program. It is occasionally created as an invisible file and is frequently destroyed when the program is quit. TMP files can also be used to temporarily store information while a new file is being constructed.


1 Answers

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

like image 56
timfjord Avatar answered Sep 28 '22 20:09

timfjord