File

The file access class, normally created by open or File.open.

Open file objects that cannot be referenced will be closed and discarded by the next garbage collection.

Superclass

Class Methods

File.mtime(filename)

Returns the file's last modified time (Time object).

If time retrieval fails, throws an Errno::EXXX exception.

File.basename(filename[, suffix])

Returns the final slash-delimited component of filename. If suffix is supplied and is identical to the end of filename, filename is returned without the suffix.

p File.basename("ruby/ruby.c")        # => "ruby.c"
p File.basename("ruby/ruby.c", ".c")  # => "ruby"
p File.basename("ruby/ruby.c", ".*")  # => "ruby"
p File.basename("ruby/ruby.exe", ".*")  # => "ruby"

See also File.dirname and File.extname.

File.delete(filename ... )

Deletes a file or files and returns the number of files deleted. If deletion fails, throws an Errno::EXXX exception.

This method is for file deletion and cannot delete directories.

File.dirname(filename)

Returns all but the final slash-delimited component of filename. Returns "." (the current directory) for a filename that does not include a slash.

p File.dirname("dir/file.ext")    # => "dir"
p File.dirname("file.ext")        # => "."
p File.dirname("foo/bar/")        # => "foo"
p File.dirname("foo//bar")        # => "foo"

See also File.basename and File.extname.

File.expand_path(path[, default_dir])

Returns a string containing path's expanded absolute path. If path is relative, sets default_dir as the base directory. If default_dir is nil or missing, uses the current directory.

p File.expand_path("..")         # => "/home/matz/work"
p File.expand_path("..", "/tmp") # => "/"
File.extname(filename)

Returns filename's extension (the string after the final dot). Dots in directory names or at the start of filenames are not considered to be denoting extensions. If filename contains no extension, returns an empty string.

p File.extname("foo/foo.txt")     # => ".txt"
p File.extname("foo/foo.tar.gz")  # => ".gz"
p File.extname("foo/bar")         # => ""
p File.extname("foo/.bar")        # => ""
p File.extname("foo.txt/bar")     # => ""
p File.extname(".foo")            # => ""

See also File.basename and File.dirname.

File.open(path[, mode])
File.open(path[, mode]) {|file| ... }

Opens the file specified by path and returns the file object. If file opening fails, throws an Errno::EXXX exception.

The mode argument is identical to the built-in function open.

Blocks can be specified for open(). When called with a block, executes the block with the given file object. The file will be closed automatically after executing the block.

When a block is specified, the return value of this method is the result of the block's evaluation.
File.rename(from, to)

Renames file, moving it to a different directory as required. If a file already exists at the destination, it is overwritten.

Returns 0 when file movement is successful; when it fails, throws an Errno::EXXX exception.

Methods

mtime

Returns the file's last modified time (Time object).

If time retrieval fails, throws an Errno::EXXX exception.

path

Returns the path of the opened file.

Converted from CHM to HTML with chm2web Pro 2.85 (unicode)