prev - up - next - index

# File

  The class for accessing files. Normally, created by `open`. This class has singleton-methods that the `FileTest` class has.

## SuperClass:

  `IO`

## Class Methods:

  `atime(filename)`

    Returns the last accessed time of the file.

  `basename(filename[, suffix])`

    Returns the last slash-delimited component of the filename. If suffix is supplied and is identical to the end of name, it is removed from filename.

    Example:

    >     basename("ruby/ruby.c")
    >     	=> "ruby.c"
    >     basename("ruby/ruby.c", ".c")
    >             => "ruby"

  `ctime(filename)`

    Returns the last status change time of the file.

  `chmod(mode, path, file...)`

    Change the mode of the `file`s to the mode. Returns the number of the file it processed. See `chmod(2)`.

  `chown(owner, group, file...)`

    Change the owner and the groups of the `file`s to the owner. Only super-user can change the owner of the file. Returns the number of the file it processed.

    The owner and the group can leave unchanged with supplying `nil` or `-1` to as the argument.

  `dirname(filename)`

    Returns all but the final slash-delimited component of filename. Returns `"."` (meaning the current directory), if filename is a single component.

  `expand_path(path[,default_dir])`

    Converts path to absolute, and canonicalized path. Second arg default_dir is directory to start with if path is relative (does not start with slash); if default_dir is nil or missing, the current directory of the process is used. An initial ``~`' expands to your home directory. An initial ``~USER`' expands to USER's home directory.

    >     expand_path("..")
    >            => "/home/matz/work"
    >     expand_path("~")
    >            => "/home/matz"
    >     expand_path("~matz")
    >            => "/home/matz"

  `ftype(filename)`

    Returns a string which describes the type of the file. The string is either:

    > `"file"`
    > `"directory"`
    > `"characterSpecial"`
    > `"blockSpecial"`
    > `"fifo"`
    > `"link"`
    > `"socket"`

  `join(item...)`

    Combines file names using `File::Separator`.

    It works just like:

        [items,..].join(File::Separator)

  `link(old, new)`

    Creates a new hard link to old (existing) file. See `link(2)`.

  `lstat(filename)`

    Does the same thing as the `stat`, but stats a symbolic link instead of the file the symbolic link points to.

  `mtime(filename)`

    Returns the last modified time of the file.

  `open(path[, mode])`
  `new(path[, mode])`

    Opens the file specified by path, and returns a `File` object associated with that file.

    The mode argument specifies the mode for the opened file, which is either `"r"`, `"r+"`, `"w"`, `"w+"`, `"a"`, `"a+"`. See `fopen(3)`. If mode omitted, the default is `"r"`.

    if mode is a integer, it is considered like the second argument to `open(2)`. One of the flags must be either `RDONLY`, `WRONLY` or `RDWR`. Flags may also be bitwise-or'd with one or more of `APPEND`, `CREAT`, `EXCL`, `NONBLOCK`, `TRUNC`, `NOCTTY`, `BINARY`.

    If a new file is created as part of opening it, permissions (an integer) is used to set the permissions for the new file in conjunction with the process's file mode creation mask. Permissions defaults to 0666.

    If `open` is called with block, the block will be executed with a newly opened file object. The file object will be closed automatically after executing the block.

  `readlink(path)`

    Returns the symbolic link path as a string.

  `rename(from, to)`

    Renames file, moving it across the directories if required. See `rename(2)`. If to-file already exists, it will be overwritten.

  `size(pathname)`

    Returns the size of the file specified by the pathname in bytes.

  `split(pathname)`

    Splits the pathname in a pair `[head, tail]`, where tail is the last pathname component and head is everything leading up to that.

  `stat(filename)`

    Returns the status info of the filename in the `Stat` structure, which has attributes as follows:

    >     dev	 	# device number of file-system
    >     ino		# i-node number
    >     mode		# file mode
    >     nlink		# number of hard links
    >     uid		# user ID of owner
    >     gid		# group ID of owner
    >     rdev		# device type (special files only)
    >     size		# total size, in bytes
    >     blksize		# preferred blocksize for file-system I/O
    >     blocks		# number of blocks allocated
    >     atime		# time of last access
    >     mtime		# time of last modification
    >     ctime		# time of last i-node change

    For more detail, see `stat(2)`. Some fields are filled with 0, if that field is not supported on your system.

  `symlink(old, new)`

    Created a new symbolic link to the old-file.

  `truncate(path, length)`

    truncate the file specified by path to at most length byte.

  `unlink(file...)`
  `delete(file...)`

    Deletes files. Returns the number of files successfully deleted. Use `Dir.rmdir` instead to delete directories.

  `umask([umask])`

    Changes umask of the user. Returns the current mask. If umask is not specified, just current returns the current mask value.

  `utime(atime, mtime, file...)`

    Change the access and modification times on each files. Returns the number of files successfully changed. The first two arguments must be either the number or the instance of the `Time` class.

  In addition, the `File` class has class methods defined in `FileTest`.

## Methods:

  `atime`

    Returns the last accessed time of the file.

  `ctime`

    Returns the last status change time of the file.

  `chmod(mode)`

    Change the mode of the `file`s to the mode. See `chmod(2)`.

  `chown(owner, group)`

    Change the owner and the groups of the `file`s to the owner. Only super-user can change the owner of the file. The owner and the group can be unchanched with supplying `nil` or `-1` to as the argument.

  `eof`
  `eof?`

    Returns true, if the file pointer reached at the end of file.

  `flock(operation)`

    Applys or removes an advisory lock on the file. `flock()` returns false if `LOCK_NB` supplied as the operation and the call would block. Valid operations are given below:

    `LOCK_SH`
      Shared lock. More than one process may hold a shared lock for a given file at a given time.
    `LOCK_EX`
      Exclusive lock. Only one process may hold an exclusive lock for a given file at a given time.
    `LOCK_UN`
      Unlock.
    `LOCK_NB`
      Don't block when locking. May be specified (by or'ing) along with one of the other operations.

    These constants are defined under the class `File`.

  `lstat`

    Does the same thing as the `stat`, but stats a symbolic link instead of the file the symbolic link points to.

  `mtime`

    Returns the last modified time of the file.

  `reopen(io)`

    Reconnect `self` to io. It also changes the class of the stream.

  `reopen(path, mode)`

    Opens and reconnects the file specified by path, with mode.

  `path`

    Returns pathname of the opened file.

  `stat`

    Returns the status info of the file in the `Stat` structure.

  `truncate(length)`

    truncate the file to at most length byte. The file must be opened in the write mode.

## Constants:

  `Separator`

    The separating character of the file path, which is normally `"/"`.

prev - up - next - index

matz@netlab.co.jp