D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Tempfile
/
Filename :
create-c.ri
back
Copy
U:RDoc::AnyMethod[iI"create:ETI"Tempfile::create;TT:publico:RDoc::Markup::Document:@parts['o:RDoc::Markup::Paragraph; [I"3Creates a file in the underlying file system; ;TI"3returns a new \File object based on that file.;To:RDoc::Markup::BlankLine o; ; [I"JWith no block given and no arguments, creates and returns file whose:;T@o:RDoc::Markup::List: @type:BULLET:@items[ o:RDoc::Markup::ListItem:@label0; [o; ; [I"4Class is {File}[rdoc-ref:File] (not \Tempfile).;To;;0; [o; ; [I"DDirectory is the system temporary directory (system-dependent).;To;;0; [o; ; [I"4Generated filename is unique in that directory.;To;;0; [o; ; [I"$Permissions are <tt>0600</tt>; ;TI"<see {File Permissions}[rdoc-ref:File@File+Permissions].;To;;0; [o; ; [I"DMode is <tt>'w+'</tt> (read/write mode, positioned at the end).;T@o; ; [I"PThe temporary file removal depends on the keyword argument +anonymous+ and ;TI"&whether a block is given or not. ;TI"FSee the description about the +anonymous+ keyword argument later.;T@o; ; [I" Example:;T@o:RDoc::Markup::Verbatim; [%I"Ef = Tempfile.create # => #<File:/tmp/20220505-9795-17ky6f6> ;TI"'f.class # => File ;TI"?f.path # => "/tmp/20220505-9795-17ky6f6" ;TI"+f.stat.mode.to_s(8) # => "100600" ;TI" f.close ;TI"'File.exist?(f.path) # => true ;TI"File.unlink(f.path) ;TI"(File.exist?(f.path) # => false ;TI" ;TI"Tempfile.create {|f| ;TI" f.puts "foo" ;TI" f.rewind ;TI"* f.read # => "foo\n" ;TI"@ f.path # => "/tmp/20240524-380207-oma0ny" ;TI"' File.exist?(f.path) # => true ;TI"B} # The file is removed at block exit. ;TI" ;TI"*f = Tempfile.create(anonymous: true) ;TI"5# The file is already removed because anonymous ;TI"Gf.path # => "/tmp/" (no filename since no file) ;TI"f.puts "foo" ;TI"f.rewind ;TI"*f.read # => "foo\n" ;TI" f.close ;TI" ;TI"+Tempfile.create(anonymous: true) {|f| ;TI"7 # The file is already removed because anonymous ;TI"G f.path # => "/tmp/" (no filename since no file) ;TI" f.puts "foo" ;TI" f.rewind ;TI"* f.read # => "foo\n" ;TI"} ;T:@format0o; ; [I"DThe argument +basename+, if given, may be one of the following:;T@o;; ;;[o;;0; [o; ; [I"=A string: the generated filename begins with +basename+:;T@o;; [I"GTempfile.create('foo') # => #<File:/tmp/foo20220505-9795-1gok8l9> ;T;0o;;0; [o; ; [I"8An array of two strings <tt>[prefix, suffix]</tt>: ;TI"Hthe generated filename begins with +prefix+ and ends with +suffix+:;T@o;; [I"RTempfile.create(%w/foo .jpg/) # => #<File:/tmp/foo20220505-17839-tnjchh.jpg> ;T;0o; ; [I"[With arguments +basename+ and +tmpdir+, the file is created in the directory +tmpdir+:;T@o;; [I"ITempfile.create('foo', '.') # => #<File:./foo20220505-9795-1emu6g8> ;T;0o; ; [I"NKeyword arguments +mode+ and +options+ are passed directly to the method ;TI"%{File.open}[rdoc-ref:File.open]:;T@o;; ;;[o;;0; [o; ; [I"3The value given for +mode+ must be an integer ;TI"Dand may be expressed as the logical OR of constants defined in ;TI"1{File::Constants}[rdoc-ref:File::Constants].;To;;0; [o; ; [I"AFor +options+, see {Open Options}[rdoc-ref:IO@Open+Options].;T@o; ; [I"IThe keyword argument +anonymous+ specifies when the file is removed.;T@o;; ;;[ o;;0; [o; ; [I"Q<tt>anonymous=false</tt> (default) without a block: the file is not removed.;To;;0; [o; ; [I"`<tt>anonymous=false</tt> (default) with a block: the file is removed after the block exits.;To;;0; [o; ; [I"S<tt>anonymous=true</tt> without a block: the file is removed before returning.;To;;0; [o; ; [I"Z<tt>anonymous=true</tt> with a block: the file is removed before the block is called.;T@o; ; [ I"CIn the first case (<tt>anonymous=false</tt> without a block), ;TI",the file is not removed automatically. ;TI"%It should be explicitly closed. ;TI"7It can be used to rename to the desired filename. ;TI"@If the file is not needed, it should be explicitly removed.;T@o; ; [I"kThe File#path method of the created file object returns the temporary directory with a trailing slash ;TI"when +anonymous+ is true.;T@o; ; [I"\When a block is given, it creates the file as described above, passes it to the block, ;TI"$and returns the block's value. ;TI"XBefore the returning, the file object is closed and the underlying file is removed:;T@o;; [I"JTempfile.create {|file| file.path } # => "/tmp/20220505-9795-rkists" ;T;0o; ; [I"Implementation note:;T@o; ; [I"^The keyword argument +anonymous=true+ is implemented using FILE_SHARE_DELETE on Windows. ;TI" O_TMPFILE is used on Linux.;T@o; ; [I"Related: Tempfile.new.;T: @fileI"lib/tempfile.rb;T:0@omit_headings_from_table_of_contents_below000[ I"L(basename="", tmpdir=nil, mode: 0, anonymous: false, **options, &block);T@�FI" Tempfile;TcRDoc::NormalClass00