class File

Files, by path. Operating-system failures (a missing file, say) raise SystemCallError; File.open with a block closes the file afterwards.

The operations on paths and whole files run on a helper thread when they may wait for the disk: only the calling task waits.

path = File.join(Dir.tmpdir, "doc_file_#{Process.pid}.txt")
File.write(path, "one\ntwo\n")             # => 8
File.readlines(path, chomp: true)          # => ["one", "two"]
File.open(path, "a") { |f| f.puts "three" }
File.read(path)                            # => "one\ntwo\nthree\n"
File.delete(path)                          # => 1
File.read(path)                            # raises SystemCallError

<prelude>:6254

Constants

SEPARATOR: String

The separator of a path's parts: "/".

<prelude>:6258

Class methods

def self.open(path: String, mode: String = …) -> IO

Opens the file at path and returns the stream.

mode is "r" (read, the default), "w" (write, created or truncated), "a" (append, created), or one of them with + to read and write too ("r+", "w+", "a+"); a trailing b is accepted and ignored. Reads and writes on such a stream share one position: a write after a read goes where the read stopped, and a read after a write starts after it ("a+" writes at the end whatever the position).

Raises

SystemCallError when the file cannot be opened, ArgumentError for another mode.

File.write("letters.txt", "abc")
File.open("letters.txt", "r+") { |f| f.read(1); f.write("Z") }
File.read("letters.txt")   # => "aZc"

<prelude>:6280

def self.open[R](path: String, mode: String = …, &: (IO) -> R) -> R

Opens the file as .open does, yields the stream, closes it when the block ends (also when it raises) and returns the block's value.

<prelude>:6283

def self.read(path: String) -> String

The whole content of the file at path.

<prelude>:6285

def self.read_bytes(path: String) -> Bytes

The whole content of the file at path, as bytes.

<prelude>:6287

def self.write(path: String, content: String) -> Int

Replaces the content of the file at path (creating it) with content, and returns the number of bytes written.

<prelude>:6290

def self.write(path: String, content: Bytes) -> Int

Replaces the content of the file at path (creating it) with content, and returns the number of bytes written.

<prelude>:6292

def self.append(path: String, content: String) -> Int

Adds content at the end of the file at path (creating it), and returns the number of bytes written.

<prelude>:6295

def self.append(path: String, content: Bytes) -> Int

Adds content at the end of the file at path (creating it), and returns the number of bytes written.

<prelude>:6297

def self.exist?(path: String) -> Bool

Whether something exists at path (a file, a directory, ...).

<prelude>:6299

def self.file?(path: String) -> Bool

Whether path is a regular file (following symbolic links).

<prelude>:6301

def self.directory?(path: String) -> Bool

Whether path is a directory (following symbolic links).

<prelude>:6303

def self.readable?(path: String) -> Bool

Whether path can be opened for reading.

<prelude>:6307

def self.writable?(path: String) -> Bool

Whether path can be opened for writing (it is not created), or is a directory that is not read-only.

<prelude>:6310

def self.executable?(path: String) -> Bool

Whether path is a regular file with an execute permission bit set (false for a directory).

<prelude>:6313

def self.size(path: String) -> Int

The size of the file at path, in bytes.

<prelude>:6315

def self.mtime(path: String) -> Time

When the file at path was last modified, in the local time zone.

<prelude>:6317

def self.delete(*paths: String) -> Int

Deletes the files at paths and returns how many it deleted.

Raises

SystemCallError at the first that cannot be deleted (the ones before it are deleted).

<prelude>:6324

def self.rename(from: String, to: String) -> Int

Renames (moves) from to to, replacing a file at to; returns 0.

<prelude>:6326

def self.copy(from: String, to: String) -> Int

Copies the file from to to, replacing a file at to, and returns the number of bytes copied.

<prelude>:6329

def self.readlines(path: String, chomp: Bool = …) -> Array[String]

The lines of the file at path, each with its newline, or without when chomp is true.

<prelude>:6332

def self.each_line(path: String, &: (String) -> Void) -> Void

Yields each line of the file at path, with its newline.

<prelude>:6334

def self.foreach(path: String, &: (String) -> Void) -> Void

Yields each line of the file at path, as .each_line.

<prelude>:6336

def self.basename(path: String, suffix: String = …) -> String

The last part of path, without trailing separators, and without suffix when it ends with it (".*": any extension).

File.basename("/a/b/c.rb")          # => "c.rb"
File.basename("/a/b/c.rb", ".rb")   # => "c"
File.basename("/a/b/c.rb", ".*")    # => "c"
File.basename("/a/b/")              # => "b"

<prelude>:6346

def self.dirname(path: String) -> String

All of path but its last part: "/a/b" for "/a/b/c.rb", "." for "c.rb", "/" for "/".

<prelude>:6349

def self.extname(path: String) -> String

The extension of the last part of path, with its dot (".gz" for "a.tar.gz"); "" when there is none, for a dot file (".bashrc") and for a name ending in a dot.

<prelude>:6353

def self.split(path: String) -> [String, String]

.dirname and .basename of path: ["/a/b", "c.rb"].

<prelude>:6355

def self.join(*parts: String) -> String

The parts joined with one "/" between them (the separators already at their ends are not doubled); "" for no parts.

File.join("a", "b", "c.rb")   # => "a/b/c.rb"
File.join("a/", "/b")         # => "a/b"

<prelude>:6363

def self.expand_path(path: String, directory: String? = …) -> String

The absolute form of path, relative to directory (the current directory by default), with . and .. resolved and a leading ~ taken as the home directory.

Symbolic links are not resolved.

File.expand_path("../b", "/a/c")     # => "/a/b"
File.expand_path("/x/./y/../z")      # => "/x/z"

<prelude>:6374

def self.absolute_path(path: String) -> String

The absolute form of path, relative to the current directory, with . and .. resolved: .expand_path without a directory.

Unlike Ruby's, it takes a leading ~ as the home directory too.

<prelude>:6379