зеркало из https://github.com/github/ruby.git
[ruby/tempfile] Enhanced RDoc for ::new and ::create (https://github.com/ruby/tempfile/pull/10)
https://github.com/ruby/tempfile/commit/a5e53aa82a
This commit is contained in:
Родитель
6923dd932b
Коммит
589f1c1d55
145
lib/tempfile.rb
145
lib/tempfile.rb
|
@ -87,50 +87,64 @@ require 'tmpdir'
|
||||||
# same Tempfile object from multiple threads then you should protect it with a
|
# same Tempfile object from multiple threads then you should protect it with a
|
||||||
# mutex.
|
# mutex.
|
||||||
class Tempfile < DelegateClass(File)
|
class Tempfile < DelegateClass(File)
|
||||||
# Creates a temporary file with permissions 0600 (= only readable and
|
|
||||||
# writable by the owner) and opens it with mode "w+".
|
# Creates a file in the underlying file system;
|
||||||
|
# returns a new \Tempfile object based on that file.
|
||||||
#
|
#
|
||||||
# It is recommended to use Tempfile.create { ... } instead when possible,
|
# If possible, consider instead using Tempfile.create, which:
|
||||||
# because that method avoids the cost of delegation and does not rely on a
|
|
||||||
# finalizer to close and unlink the file, which is unreliable.
|
|
||||||
#
|
#
|
||||||
# The +basename+ parameter is used to determine the name of the
|
# - Avoids the performance cost of delegation,
|
||||||
# temporary file. You can either pass a String or an Array with
|
# incurred when Tempfile.new calls its superclass <tt>DelegateClass(File)</tt>.
|
||||||
# 2 String elements. In the former form, the temporary file's base
|
# - Does not rely on a finalizer to close and unlink the file,
|
||||||
# name will begin with the given string. In the latter form,
|
# which can be unreliable.
|
||||||
# the temporary file's base name will begin with the array's first
|
|
||||||
# element, and end with the second element. For example:
|
|
||||||
#
|
#
|
||||||
# file = Tempfile.new('hello')
|
# Creates and returns file whose:
|
||||||
# file.path # => something like: "/tmp/hello2843-8392-92849382--0"
|
|
||||||
#
|
#
|
||||||
# # Use the Array form to enforce an extension in the filename:
|
# - Class is \Tempfile (not \File, as in Tempfile.create).
|
||||||
# file = Tempfile.new(['hello', '.jpg'])
|
# - Directory is the system temporary directory (system-dependent).
|
||||||
# file.path # => something like: "/tmp/hello2843-8392-92849382--0.jpg"
|
# - Generated filename is unique in that directory.
|
||||||
|
# - Permissions are <tt>0600</tt>;
|
||||||
|
# see {File Permissions}[https://docs.ruby-lang.org/en/master/File.html#label-File+Permissions].
|
||||||
|
# - Mode is <tt>'w+'</tt> (read/write mode, positioned at the end).
|
||||||
#
|
#
|
||||||
# The temporary file will be placed in the directory as specified
|
# The underlying file is removed when the \Tempfile object dies
|
||||||
# by the +tmpdir+ parameter. By default, this is +Dir.tmpdir+.
|
# and is reclaimed by the garbage collector.
|
||||||
#
|
#
|
||||||
# file = Tempfile.new('hello', '/home/aisaka')
|
# Example:
|
||||||
# file.path # => something like: "/home/aisaka/hello2843-8392-92849382--0"
|
|
||||||
#
|
#
|
||||||
# You can also pass an options hash. Under the hood, Tempfile creates
|
# f = Tempfile.new # => #<Tempfile:/tmp/20220505-17839-1s0kt30>
|
||||||
# the temporary file using +File.open+. These options will be passed to
|
# f.class # => Tempfile
|
||||||
# +File.open+. This is mostly useful for specifying encoding
|
# f.path # => "/tmp/20220505-17839-1s0kt30"
|
||||||
# options, e.g.:
|
# f.stat.mode.to_s(8) # => "100600"
|
||||||
|
# File.exist?(f.path) # => true
|
||||||
|
# File.unlink(f.path) #
|
||||||
|
# File.exist?(f.path) # => false
|
||||||
#
|
#
|
||||||
# Tempfile.new('hello', '/home/aisaka', encoding: 'ascii-8bit')
|
# Argument +basename+, if given, may be one of:
|
||||||
#
|
#
|
||||||
# # You can also omit the 'tmpdir' parameter:
|
# - A string: the generated filename begins with +basename+:
|
||||||
# Tempfile.new('hello', encoding: 'ascii-8bit')
|
|
||||||
#
|
#
|
||||||
# Note: +mode+ keyword argument, as accepted by Tempfile, can only be
|
# Tempfile.new('foo') # => #<Tempfile:/tmp/foo20220505-17839-1whk2f>
|
||||||
# numeric, combination of the modes defined in File::Constants.
|
|
||||||
#
|
#
|
||||||
# === Exceptions
|
# - An array of two strings <tt>[prefix, suffix]</tt>:
|
||||||
|
# the generated filename begins with +prefix+ and ends with +suffix+:
|
||||||
|
#
|
||||||
|
# Tempfile.new(%w/foo .jpg/) # => #<Tempfile:/tmp/foo20220505-17839-58xtfi.jpg>
|
||||||
|
#
|
||||||
|
# With arguments +basename+ and +tmpdir+, the file is created in directory +tmpdir+:
|
||||||
|
#
|
||||||
|
# Tempfile.new('foo', '.') # => #<Tempfile:./foo20220505-17839-xfstr8>
|
||||||
|
#
|
||||||
|
# Keyword arguments +mode+ and +options+ are passed directly to method
|
||||||
|
# {File.open}[https://docs.ruby-lang.org/en/master/File.html#method-c-open]:
|
||||||
|
#
|
||||||
|
# - The value given with +mode+ must be an integer,
|
||||||
|
# and may be expressed as the logical OR of constants defined in
|
||||||
|
# {File::Constants}[https://docs.ruby-lang.org/en/master/File/Constants.html].
|
||||||
|
# - For +options+, see {Open Options}[https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Open+Options].
|
||||||
|
#
|
||||||
|
# Related: Tempfile.create.
|
||||||
#
|
#
|
||||||
# If Tempfile.new cannot find a unique filename within a limited
|
|
||||||
# number of tries, then it will raise an exception.
|
|
||||||
def initialize(basename="", tmpdir=nil, mode: 0, **options)
|
def initialize(basename="", tmpdir=nil, mode: 0, **options)
|
||||||
warn "Tempfile.new doesn't call the given block.", uplevel: 1 if block_given?
|
warn "Tempfile.new doesn't call the given block.", uplevel: 1 if block_given?
|
||||||
|
|
||||||
|
@ -325,26 +339,61 @@ class Tempfile < DelegateClass(File)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a temporary file as a usual File object (not a Tempfile).
|
# Creates a file in the underlying file system;
|
||||||
# It does not use finalizer and delegation, which makes it more efficient and reliable.
|
# returns a new \File object based on that file.
|
||||||
#
|
#
|
||||||
# If no block is given, this is similar to Tempfile.new except
|
# With no block given and no arguments, creates and returns file whose:
|
||||||
# creating File instead of Tempfile. In that case, the created file is
|
|
||||||
# not removed automatically. You should use File.unlink to remove it.
|
|
||||||
#
|
#
|
||||||
# If a block is given, then a File object will be constructed,
|
# - Class is {File}[https://docs.ruby-lang.org/en/master/File.html] (not \Tempfile).
|
||||||
# and the block is invoked with the object as the argument.
|
# - Directory is the system temporary directory (system-dependent).
|
||||||
# The File object will be automatically closed and
|
# - Generated filename is unique in that directory.
|
||||||
# the temporary file is removed after the block terminates,
|
# - Permissions are <tt>0600</tt>;
|
||||||
# releasing all resources that the block created.
|
# see {File Permissions}[https://docs.ruby-lang.org/en/master/File.html#label-File+Permissions].
|
||||||
# The call returns the value of the block.
|
# - Mode is <tt>'w+'</tt> (read/write mode, positioned at the end).
|
||||||
#
|
#
|
||||||
# In any case, all arguments (+basename+, +tmpdir+, +mode+, and
|
# With no block, the file is not removed automatically,
|
||||||
# <code>**options</code>) will be treated the same as for Tempfile.new.
|
# and so should be explicitly removed.
|
||||||
#
|
#
|
||||||
# Tempfile.create('foo', '/home/temp') do |f|
|
# Example:
|
||||||
# # ... do something with f ...
|
#
|
||||||
# end
|
# f = Tempfile.create # => #<File:/tmp/20220505-9795-17ky6f6>
|
||||||
|
# f.class # => File
|
||||||
|
# f.path # => "/tmp/20220505-9795-17ky6f6"
|
||||||
|
# f.stat.mode.to_s(8) # => "100600"
|
||||||
|
# File.exist?(f.path) # => true
|
||||||
|
# File.unlink(f.path)
|
||||||
|
# File.exist?(f.path) # => false
|
||||||
|
#
|
||||||
|
# Argument +basename+, if given, may be one of:
|
||||||
|
#
|
||||||
|
# - A string: the generated filename begins with +basename+:
|
||||||
|
#
|
||||||
|
# Tempfile.create('foo') # => #<File:/tmp/foo20220505-9795-1gok8l9>
|
||||||
|
#
|
||||||
|
# - An array of two strings <tt>[prefix, suffix]</tt>:
|
||||||
|
# the generated filename begins with +prefix+ and ends with +suffix+:
|
||||||
|
#
|
||||||
|
# Tempfile.create(%w/foo .jpg/) # => #<File:/tmp/foo20220505-17839-tnjchh.jpg>
|
||||||
|
#
|
||||||
|
# With arguments +basename+ and +tmpdir+, the file is created in directory +tmpdir+:
|
||||||
|
#
|
||||||
|
# Tempfile.create('foo', '.') # => #<File:./foo20220505-9795-1emu6g8>
|
||||||
|
#
|
||||||
|
# Keyword arguments +mode+ and +options+ are passed directly to method
|
||||||
|
# {File.open}[https://docs.ruby-lang.org/en/master/File.html#method-c-open]:
|
||||||
|
#
|
||||||
|
# - The value given with +mode+ must be an integer,
|
||||||
|
# and may be expressed as the logical OR of constants defined in
|
||||||
|
# {File::Constants}[https://docs.ruby-lang.org/en/master/File/Constants.html].
|
||||||
|
# - For +options+, see {Open Options}[https://docs.ruby-lang.org/en/master/IO.html#class-IO-label-Open+Options].
|
||||||
|
#
|
||||||
|
# With a block given, creates the file as above, passes it to the block,
|
||||||
|
# and returns the block's value;
|
||||||
|
# before the return, the file object is closed and the underlying file is removed:
|
||||||
|
#
|
||||||
|
# Tempfile.create {|file| file.path } # => "/tmp/20220505-9795-rkists"
|
||||||
|
#
|
||||||
|
# Related: Tempfile.new.
|
||||||
#
|
#
|
||||||
def Tempfile.create(basename="", tmpdir=nil, mode: 0, **options)
|
def Tempfile.create(basename="", tmpdir=nil, mode: 0, **options)
|
||||||
tmpfile = nil
|
tmpfile = nil
|
||||||
|
|
Загрузка…
Ссылка в новой задаче