Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ruby File Class and Methods

Ruby File Class and Methods

File represents an object connected to a regular file stdio object. open returns an instance of this class for a regular file.

Class Methods

No. Method & Description
1 File::atime( path)
Return path the last access time of.
2 File::basename( path[, suffix])
 返回 *path* 末尾的文件名。如果指定了 *suffix*,则它会从文件名末尾被删除。       
 例如:File.basename("/home/users/bin/ruby.exe") #=> "ruby.exe" |

| 3 | File::blockdev?( path)
Returns true if path is a block device. | | 4 | File::chardev?( path)
Returns true if path is a character device. | | 5 | File::chmod( mode, path...)
Changes the permission mode of the specified file. | | 6 | File::chown( owner, group, path...)
Changes the owner and group of the specified file. | | 7 | File::ctime( path)
Returns the last inode change time of path. | | 8 | File::delete( path...)
File::unlink( path...)

Deletes the specified file. | | 9 | File::directory?( path)
Returns true if path is a directory. | | 10 | File::dirname( path)
Returns the directory part of path, excluding the final file name. | | 11 | File::executable?( path)
Returns true if path is executable. | | 12 | File::executable_real?( path)
Returns true if path is executable by the real user's permissions. | | 13 | File::exist?( path)
Returns true if path exists. | | 1 | File::expand_path( path[, dir])
Returns the absolute path of path, expanding ~ to the process owner's home directory and ~user to that user's home directory. Relative paths are relative to the directory specified by dir, or to the current working directory if dir is omitted. | | 14 | File::file?( path)
Returns true if path is a regular file. | | 15 | File::ftype( path)
Returns one of the following strings indicating the file type:

| | 16 | **File::grpowned?( path)** Returns true if path is owned by the user's group. | | 17 | **File::join( item...)** Returns a string formed by joining the specified items together, separated by File::Separator. For example: File::join("", "home", "usrs", "bin") # => "/home/usrs/bin" | | 18 | **File::link( old, new)** Creates a hard link to the file old. | | 19 | **File::lstat( path)** Same as stat, but returns information about the symbolic link itself rather than the file it points to. | | 20 | **File::mtime( path)** Returns the last modification time of path. | | 21 | **File::new( path[, mode="r"]) File::open( path[, mode="r"]) File::open( path[, mode="r"]) {\|f\| ...}** Opens a file. If a block is specified, the block is executed passing the new file as a parameter. When the block exits, the file is closed automatically. These methods differ from Kernel.open in that even if path begins with \|, the following string is not run as a command. | | 22 | **File::owned?( path)** Returns true if path is owned by a valid user. | | 23 | **File::pipe?( path)** Returns true if path is a pipe. | | 24 | **File::readable?( path)** Returns true if path is readable. | | 25 | **File::readable_real?( path)** Returns true if path is readable by the real user's permissions. | | 25 | **File::readlink( path)** Returns the file that path points to. | | 26 | **File::rename( old, new)** Changes the file name old to new. | | 27 | **File::setgid?( path)** Returns true if the set-group-id permission bit is set for path. | | 28 | **File::setuid?( path)** Returns true if the set-user-id permission bit is set for path. | | 29 | **File::size( path)** Returns the file size of path. | | 30 | **File::size?( path)** Returns the file size of path, or nil if it is 0. | | 31 | **File::socket?( path)** Returns true if path is a socket. | | 32 | **File::split( path)** Returns an array containing the contents of path, split into File::dirname(path) and File::basename(path). | | 33 | **File::stat( path)** Returns a File::Stat object with information about path. | | 34 | **File::sticky?( path)** Returns true if the sticky bit is set for path. | | 35 | **File::symlink( old, new)** Creates a symbolic link to the file old. | | 36 | **File::symlink?( path)** Returns true if path is a symbolic link. | | 37 | **File::truncate( path, len)** Truncates the specified file to len bytes. | | 38 | **File::unlink( path...)** Deletes the files given by path. | | 39 | **File::umask([ mask])** If no argument is specified, returns the current umask for this process. If an argument is specified, sets the umask and returns the old umask. | | 40 | **File::utime( atime, mtime, path...)** Changes the access and modification times of the specified files. | | 41 | **File::writable?( path)** Returns true if path is writable. | | 42 | **File::writable_real?( path)** Returns true if path is writable by the real user's permissions. | | 43 | **File::zero?( path)** Returns true if the file size of path is 0. |

Instance Methods

Assume f Is File An instance of the class:

No. Method & Description
1 f.atime
Returns the last access time of f.
2 f.chmode( mode)
Changes the permission mode of f.
3 f.chown( owner, group)
Change the owner and group of f.
4 f.ctime
Returns the time of the last inode change of f.
5 f.flock( op)
Calls flock(2). op can be 0 or a logical value or the File class constants LOCK_EX, LOCK_NB, LOCK_SH, and LOCK_UN.
6 f.lstat
Same as stat, but it returns information about the symbolic link itself rather than the file it points to.
7 f.mtime
Returns the last modification time of f.
8 f.path
Returns the pathname used to create f.
9 f.reopen( path[, mode="r"])
Reopens the file.
10 f.truncate( len)
Truncates f to len bytes.