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:

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:

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]:

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

Class Method Details

.serializersObject



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.

Parameters:

  • basename (String)

    Basename of the temporary file.

  • tmpdir (String) (defaults to: Dir.tmpdir)

    Parent directory of the temporary file.

Returns:



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