Module: Chan
- Defined in:
- lib/xchan/tempfile.rb,
lib/xchan.rb,
lib/xchan/version.rb
Overview
Creates a file in the underlying file system; returns a new \File object based on that file.
With no block given and no arguments, creates and returns file whose:
- Class is Filehttps://docs.ruby-lang.org/en/master/File.html.
- Directory is the system temporary directory (system-dependent).
- Generated filename is unique in that directory.
- Permissions are 0600; see Permissions[https://docs.ruby-lang.org/en/master/File.html#label-File+Permissions].
- Mode is 'w+' (read/write mode, positioned at the end).
With no block, the file is not removed automatically, and so should be explicitly removed.
Example:
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 [prefix, suffix]: 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 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.
Defined Under Namespace
Modules: Mixin Classes: Bytes, Stat, Tempfile, UNIXSocket
Constant Summary collapse
- WaitReadable =
Class.new(IO::EAGAINWaitReadable)
- WaitWritable =
Class.new(IO::EAGAINWaitWritable)
- WaitLockable =
Class.new(Errno::EWOULDBLOCK)
- Plain =
Class.new do def self.dump(str) = str.to_s def self.load(str) = str.to_s end
- VERSION =
"0.16.5"
Class Method Summary collapse
- .serializers ⇒ Object
-
.temporary_file(basename, tmpdir: Dir.tmpdir) ⇒ Chan::Tempfile
Returns an unlinked Chan::Tempfile object that can be read from, and written to by the process that created it, inclusive of its child processes, but not of processes other than that.
Class Method Details
.serializers ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/xchan.rb', line 35 def self.serializers { plain: lambda { Plain }, marshal: lambda { Marshal }, json: lambda { require "json" unless defined?(JSON) JSON }, yaml: lambda { require "yaml" unless defined?(YAML) YAML } } end |
.temporary_file(basename, tmpdir: Dir.tmpdir) ⇒ Chan::Tempfile
Returns an unlinked Chan::Tempfile object that can be read from, and written to by the process that created it, inclusive of its child processes, but not of processes other than that.
31 32 33 |
# File 'lib/xchan.rb', line 31 def self.temporary_file(basename, tmpdir: Dir.tmpdir) Chan::Tempfile.new(basename, tmpdir, perm: 0).tap(&:unlink) end |