Class: File::Temp

Inherits:
File
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/file/temp_java.rb,
lib/file/temp_c.rb

Constant Summary collapse

VERSION =

The version of the file-temp library.

'1.2.1'
TMPDIR =

The temporary directory used on MS Windows or Unix.

ENV['TEMP'] || ENV['TMP'] || ENV['TMPDIR'] || Dir.tmpdir

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

The template argument is ignored if the delete argument is true.

Example:

fh = File::Temp.new(true, 'rb_file_temp_XXXXXX') => file
fh.puts 'hello world'
fh.close


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/file/temp_java.rb', line 37

def initialize(delete = true, template = 'rb_file_temp_XXXXXX')
  raise TypeError unless template.is_a?(String)

  # Since Java uses a GUID extension to generate a unique file name
  # we'll simply chop off the 'X' characters and let Java do the rest.
  template = template.sub(/_X{1,6}/, '_')

  # For consistency between implementations, convert errors here
  # to Errno::EINVAL.
  begin
    @file = java.io.File.createTempFile(template, nil)
  rescue NativeException => err
    raise SystemCallError.new(22), template # 22 is EINVAL
  end

  @file.deleteOnExit if delete

  @path = @file.getName

  super(@path, 'wb+')
end

Instance Attribute Details

#pathObject (readonly)

The name of the temporary file. Set to nil if the delete option to the constructor is true.



12
13
14
# File 'lib/file/temp_java.rb', line 12

def path
  @path
end

Class Method Details

.temp_nameObject

Generates a unique file name.

Note that a file is not actually generated on the filesystem. – NOTE: One quirk of the Windows function is that, after the first call, it adds a file extension of sequential numbers in base 32, e.g. .1-.1vvvvvu.



61
62
63
64
65
66
67
# File 'lib/file/temp_java.rb', line 61

def self.temp_name
  file = java.io.File.createTempFile('rb_file_temp_', nil)
  file.deleteOnExit
  name = file.getName
  file.finalize
  name
end

Instance Method Details

#closeObject

The close method was overridden to ensure the internal file pointer we created in the constructor is closed. It is otherwise identical to the File#close method.



72
73
74
75
# File 'lib/file/temp_java.rb', line 72

def close
  super
  @file.finalize
end