Class: File::Temp
- Inherits:
-
File
- Object
- File
- File::Temp
- Extended by:
- FFI::Library
- Defined in:
- lib/file/windows/temp.rb,
lib/file/unix/temp.rb,
lib/file/java/temp.rb
Constant Summary collapse
- VERSION =
The version of the file-temp library.
'1.3.0'- TMPDIR =
The temporary directory used on MS Windows or Unix.
java.lang.System.getProperties["java.io.tmpdir"]
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
The name of the temporary file.
Class Method Summary collapse
-
.temp_name ⇒ Object
Generates a unique file name.
Instance Method Summary collapse
-
#close ⇒ Object
Identical to the File#close method except that we also finalize the underlying Java File object.
-
#initialize(delete = true, template = 'rb_file_temp_XXXXXX') ⇒ Temp
constructor
Creates a new, anonymous, temporary file in your File::Temp::TMPDIR directory.
Constructor Details
#initialize(delete = true, template = 'rb_file_temp_XXXXXX') ⇒ Temp
Creates a new, anonymous, temporary file in your File::Temp::TMPDIR directory.
If the delete option is set to true (the default) then the temporary file will be deleted automatically as soon as all references to it are closed. Otherwise, the file will live on in your File::Temp::TMPDIR path.
If the delete option is set to false, then the file is not deleted. In addition, you can supply a string template that the system replaces with a unique filename. This template should end with 3 to 6 ‘X’ characters. The default template is ‘rb_file_temp_XXXXXX’. In this case the temporary file lives in the directory where it was created.
Note that when using JRuby the template naming is not as strict, and the trailing ‘X’ characters are simply replaced with the GUID that Java generates for unique file names.
Example:
fh = File::Temp.new(true, 'rb_file_temp_XXXXXX') => file
fh.puts 'hello world'
fh.close
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/file/windows/temp.rb', line 87 def initialize(delete = true, template = 'rb_file_temp_XXXXXX') @fptr = nil if delete @fptr = tmpfile() fd = _fileno(@fptr) else begin omask = File.umask(077) ptr = FFI::MemoryPointer.from_string(template) errno = mktemp_s(ptr, ptr.size) raise SystemCallError.new('mktemp_s', errno) if errno != 0 @path = File.join(TMPDIR, ptr.read_string) @path.tr!(File::SEPARATOR, File::ALT_SEPARATOR) ensure File.umask(omask) end end if delete super(fd, 'wb+') else super(@path, 'wb+') end end |
Instance Attribute Details
#path ⇒ Object (readonly)
The name of the temporary file.
64 65 66 |
# File 'lib/file/windows/temp.rb', line 64 def path @path end |
Class Method Details
.temp_name ⇒ Object
Generates a unique file name.
133 134 135 136 137 138 139 140 |
# File 'lib/file/windows/temp.rb', line 133 def self.temp_name ptr = FFI::MemoryPointer.new(:char, 1024) errno = tmpnam_s(ptr, ptr.size) raise SystemCallError.new('tmpnam_s', errno) if errno != 0 TMPDIR + ptr.read_string + 'tmp' end |
Instance Method Details
#close ⇒ Object
Identical to the File#close method except that we also finalize the underlying Java File object.
121 122 123 124 |
# File 'lib/file/windows/temp.rb', line 121 def close super fclose(@fptr) if @fptr end |