class Tempfile

Overview

The Tempfile class is for managing temporary files. Every tempfile is operated as a File, including initializing, reading and writing.

tempfile = Tempfile.new("foo")
# or
tempfile = Tempfile.open("foo") do |file|
  file.print("foobar")
end

File.size(tempfile.path)                   # => 6
File.info(tempfile.path).modification_time # => 2015-10-20 13:11:12 UTC
File.exists?(tempfile.path)                # => true
File.read_lines(tempfile.path)             # => ["foobar"]

Files created from this class are stored in a directory that handles temporary files.

Tempfile.new("foo").path # => "/tmp/foo.ulBCPS"

Also, it is encouraged to delete a tempfile after using it, which ensures they are not left behind in your filesystem until garbage collected.

tempfile = Tempfile.new("foo")
tempfile.delete

The optional extension argument can be used to force the resulting filename to end with the given extension.

Tempfile.new("foo", ".png").path # => "/tmp/foo.ulBCPS.png"

Defined in:

tempfile.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class File

flock_exclusive(blocking = true, &block)
flock_exclusive(blocking = true)
flock_exclusive
, flock_shared(blocking = true, &block)
flock_shared(blocking = true)
flock_shared
, flock_unlock flock_unlock, inspect(io) inspect, path : String path, read_at(offset, bytesize, &block) read_at, size size, truncate(size = 0) : Nil truncate

Constructor methods inherited from class File

new(filename : String, mode = "r", perm = DEFAULT_CREATE_PERMISSIONS, encoding = nil, invalid = nil) new, open(filename, mode = "r", perm = DEFAULT_CREATE_PERMISSIONS, encoding = nil, invalid = nil) : self open

Class methods inherited from class File

basename(path) : String
basename(path, suffix) : String
basename
, chmod(path, permissions : Int | Permissions) chmod, chown(path, uid : Int = -1, gid : Int = -1, follow_symlinks = false) chown, delete(path) delete, directory?(path) : Bool directory?, dirname(path) : String dirname, each_line(filename, encoding = nil, invalid = nil, chomp = true)
each_line(filename, encoding = nil, invalid = nil, chomp = true, &block)
each_line
, empty?(path) : Bool empty?, executable?(path) : Bool executable?, exists?(path) : Bool exists?, expand_path(path, dir = nil) : String expand_path, extname(filename) : String extname, file?(path) : Bool file?, info(path, follow_symlinks = true) : Info info, info?(path : String, follow_symlinks = true) : Info? info?, join(parts : Array | Tuple) : String
join(*parts) : String
join
, link(old_path, new_path) link, match?(pattern : String, path : String) match?, open(filename, mode = "r", perm = DEFAULT_CREATE_PERMISSIONS, encoding = nil, invalid = nil, &block) open, read(filename, encoding = nil, invalid = nil) : String read, read_lines(filename, encoding = nil, invalid = nil, chomp = true) : Array(String) read_lines, readable?(path) : Bool readable?, real_path(path) : String real_path, rename(old_filename, new_filename) : Nil rename, same?(path1 : String, path2 : String, follow_symlinks = false) : Bool same?, size(filename) : UInt64 size, symlink(old_path, new_path) symlink, symlink?(path) : Bool symlink?, touch(filename : String, time : Time = Time.now) touch, utime(atime : Time, mtime : Time, filename : String) : Nil utime, writable?(path) : Bool writable?, write(filename, content, perm = DEFAULT_CREATE_PERMISSIONS, encoding = nil, invalid = nil, mode = "w") write

Class methods inherited from module Crystal::System::File

chmod(path, mode) chmod, chown(path, uid : Int, gid : Int, follow_symlinks) chown, delete(path) delete, executable?(path) : Bool executable?, exists?(path) exists?, info?(path : String, follow_symlinks : Bool) : ::File::Info? info?, link(old_path, new_path) link, mktemp(name, extension) mktemp, open(filename, mode, perm) open, readable?(path) : Bool readable?, real_path(path) real_path, rename(old_filename, new_filename) rename, symlink(old_path, new_path) symlink, tempdir tempdir, utime(atime : ::Time, mtime : ::Time, filename : String) : Nil utime, writable?(path) : Bool writable?

Instance methods inherited from class IO::FileDescriptor

blocking blocking, blocking=(value) blocking=, close_on_exec=(value : Bool) close_on_exec=, close_on_exec? close_on_exec?, closed? : Bool closed?, cooked(&block) cooked, cooked! cooked!, fcntl(cmd, arg = 0) fcntl, fd : Int32 fd, finalize finalize, info info, inspect(io) inspect, noecho(&block) noecho, noecho! noecho!, pos pos, pos=(value) pos=, pretty_print(pp) pretty_print, raw(&block) raw, raw! raw!, reopen(other : IO::FileDescriptor) reopen, seek(offset, whence : Seek = Seek::Set)
seek(offset, whence : Seek = Seek::Set, &block)
seek
, tty? tty?

Constructor methods inherited from class IO::FileDescriptor

new(fd, blocking = false) new

Class methods inherited from class IO::FileDescriptor

fcntl(fd, cmd, arg = 0) fcntl

Instance methods inherited from module IO::Buffered

close : Nil close, flush flush, flush_on_newline=(flush_on_newline) flush_on_newline=, flush_on_newline? flush_on_newline?, peek : Bytes? peek, read(slice : Bytes) read, rewind rewind, sync=(sync) sync=, sync? sync?, unbuffered_close unbuffered_close, unbuffered_flush unbuffered_flush, unbuffered_read(slice : Bytes) unbuffered_read, unbuffered_rewind unbuffered_rewind, unbuffered_write(slice : Bytes) unbuffered_write, write(slice : Bytes) write

Class methods inherited from module Crystal::System::FileDescriptor

fcntl(fd, cmd, arg = 0) fcntl

Instance methods inherited from module IO::Syscall

read_syscall_helper(slice : Bytes, errno_msg : String, &block) : Int32 read_syscall_helper, read_timeout : Time::Span? read_timeout, read_timeout=(read_timeout : Number) : Number
read_timeout=(timeout : Time::Span?) : Time::Span?
read_timeout=
, write_syscall_helper(slice : Bytes, errno_msg : String, &block) : Nil write_syscall_helper, write_timeout : Time::Span? write_timeout, write_timeout=(write_timeout : Number) : Number
write_timeout=(timeout : Time::Span?) : Time::Span?
write_timeout=

Instance methods inherited from class IO

<<(obj) : self <<, close close, closed? closed?, each_byte(&block) : Nil
each_byte
each_byte
, each_char(&block) : Nil
each_char
each_char
, each_line(*args, **options, &block) : Nil
each_line(*args, **options)
each_line
, encoding : String encoding, flush flush, gets(delimiter : Char, limit : Int, chomp = false) : String?
gets(delimiter : Char, chomp = false) : String?
gets(chomp = true) : String?
gets(limit : Int, chomp = false) : String?
gets(delimiter : String, chomp = false) : String?
gets
, gets_to_end : String gets_to_end, peek : Bytes? peek, pos pos, pos=(value) pos=, print(*objects : _) : Nil
print(obj) : Nil
print
, printf(format_string, *args) : Nil
printf(format_string, args : Array | Tuple) : Nil
printf
, puts(*objects : _) : Nil
puts : Nil
puts(obj) : Nil
puts(string : String) : Nil
puts
, read(slice : Bytes) read, read_at(offset, bytesize, &block) read_at, read_byte : UInt8? read_byte, read_bytes(type, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) read_bytes, read_char : Char? read_char, read_fully(slice : Bytes) read_fully, read_fully?(slice : Bytes) read_fully?, read_line(*args, **options) : String read_line, read_string(bytesize : Int) : String read_string, read_utf8(slice : Bytes) read_utf8, read_utf8_byte read_utf8_byte, rewind rewind, seek(offset, whence : Seek = Seek::Set) seek, set_encoding(encoding : String, invalid : Symbol? = nil) set_encoding, skip(bytes_count : Int) : Nil skip, skip_to_end : Nil skip_to_end, tell tell, tty? : Bool tty?, write(slice : Bytes) : Nil write, write_byte(byte : UInt8) write_byte, write_bytes(object, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) write_bytes, write_utf8(slice : Bytes) write_utf8

Class methods inherited from class IO

copy(src, dst, limit : Int)
copy(src, dst)
copy
, pipe(read_blocking = false, write_blocking = false) : Tuple(IO::FileDescriptor, IO::FileDescriptor)
pipe(read_blocking = false, write_blocking = false, &block)
pipe

Instance methods inherited from class Reference

==(other : self)
==(other : JSON::Any)
==(other : YAML::Any)
==(other)
==
, dup dup, hash(hasher) hash, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, pretty_print(pp) : Nil pretty_print, same?(other : Reference)
same?(other : Nil)
same?
, to_s(io : IO) : Nil to_s

Constructor methods inherited from class Reference

new new

Instance methods inherited from class Object

!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other)
===
, =~(other) =~, class class, dup dup, hash(hasher)
hash
hash
, inspect(io : IO)
inspect
inspect
, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json
to_json
, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ")
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as

Constructor methods inherited from class Object

from_json(string_or_io, root : String) : self
from_json(string_or_io) : self
from_json
, from_yaml(string_or_io : String | IO) : self from_yaml

Constructor Detail

def self.new(name, extension = nil, encoding = nil, invalid = nil) #

Creates a Tempfile with the given filename and extension.

encoding and invalid are passed to IO#set_encoding.


[View source]

Class Method Detail

def self.dirname : String #

Returns the tmp dir used for tempfile.

Tempfile.dirname # => "/tmp"

[View source]
def self.open(filename, extension = nil, &block) #

Creates a file with filename and extension, and yields it to the given block. It is closed and returned at the end of this method call.

tempfile = Tempfile.open("foo") do |file|
  file.print("bar")
end
File.read(tempfile.path) # => "bar"

[View source]
def self.tempname(extension = nil) #

Returns a fully-qualified path to a temporary file without actually creating the file.

Tempfile.tempname # => "/tmp/20171206-1234-449386"

The optional extension argument can be used to make the resulting filename to end with the given extension.

Tempfile.tempname(".sock") # => "/tmp/20171206-1234-449386.sock"

[View source]

Instance Method Detail

def delete #

Deletes this tempfile.


[View source]
def path : String #

Retrieves the full path of a this tempfile.

Tempfile.new("foo").path # => "/tmp/foo.ulBCPS"

[View source]