Class: File
Overview
A File
is an abstraction of any file object accessible by the program and is closely associated with class IO
File
includes the methods of module FileTest
as class methods, allowing you to write (for example) File.exist?("foo")
.
In the description of File methods, permission bits are a platform-specific set of bits that indicate permissions of a file. On Unix-based systems, permissions are viewed as a set of three octets, for the owner, the group, and the rest of the world. For each of these entities, permissions may be set to read, write, or execute the file:
The permission bits 0644
(in octal) would thus be interpreted as read/write for owner, and read-only for group and other. Higher-order bits may also be used to indicate the type of file (plain, directory, pipe, socket, and so on) and various other special features. If the permissions are for a directory, the meaning of the execute bit changes; when set the directory can be searched.
On non-Posix operating systems, there may be only the ability to make a file read-only or read-write. In this case, the remaining permission bits will be synthesized to resemble typical values. For instance, on Windows NT the default permission bits are 0644
, which means read/write for owner, read-only for all others. The only change that can be made is to make the file read-only, which is reported as 0444
.
Defined Under Namespace
Modules: Constants Classes: Stat
Constant Summary collapse
- Separator =
separator
- SEPARATOR =
separator
- ALT_SEPARATOR =
Qnil
- PATH_SEPARATOR =
rb_obj_freeze(rb_str_new2(PATH_SEP))
Class Method Summary collapse
-
.atime(file_name) ⇒ Time
Returns the last access time for the named file as a Time object).
-
.basename(file_name[, suffix]) ⇒ Object
Returns the last component of the filename given in file_name, which must be formed using forward slashes (“
/
”) regardless of the separator used on the local file system. -
.blockdev?(file_name) ⇒ Boolean
Returns
true
if the named file is a block device. -
.chardev?(file_name) ⇒ Boolean
Returns
true
if the named file is a character device. -
.chmod(mode_int, file_name, ...) ⇒ Integer
Changes permission bits on the named file(s) to the bit pattern represented by mode_int.
-
.chown(owner_int, group_int, file_name, ...) ⇒ Integer
Changes the owner and group of the named file(s) to the given numeric owner and group id’s.
-
.ctime(file_name) ⇒ Time
Returns the change time for the named file (the time at which directory information about the file was changed, not the file itself).
-
.delete ⇒ Object
Deletes the named files, returning the number of names passed as arguments.
-
.directory?(file_name) ⇒ Boolean
Returns
true
if the named file is a directory,false
otherwise. -
.dirname(file_name) ⇒ Object
Returns all components of the filename given in file_name except the last one.
-
.executable?(file_name) ⇒ Boolean
Returns
true
if the named file is executable by the effective user id of this process. -
.executable_real?(file_name) ⇒ Boolean
Returns
true
if the named file is executable by the real user id of this process. -
.exist? ⇒ Object
Return
true
if the named file exists. -
.exists? ⇒ Object
Return
true
if the named file exists. -
.expand_path(file_name[, dir_string]) ⇒ Object
Converts a pathname to an absolute pathname.
-
.extname(path) ⇒ String
Returns the extension (the portion of file name in path after the period).
-
.file?(file_name) ⇒ Boolean
Returns
true
if the named file exists and is a regular file. -
.fnmatch ⇒ Object
Returns true if path matches against pattern The pattern is not a regular expression; instead it follows rules similar to shell filename globbing.
-
.fnmatch? ⇒ Object
Returns true if path matches against pattern The pattern is not a regular expression; instead it follows rules similar to shell filename globbing.
-
.ftype(file_name) ⇒ String
Identifies the type of the named file; the return string is one of “
file
”, “directory
”, “characterSpecial
”, “blockSpecial
”, “fifo
”, “link
”, “socket
”, or “unknown
”. -
.grpowned?(file_name) ⇒ Boolean
Returns
true
if the named file exists and the effective group id of the calling process is the owner of the file. -
.identical?(file_1, file_2) ⇒ Boolean
Returns
true
if the named files are identical. -
.join(string, ...) ⇒ Object
Returns a new string formed by joining the strings using
File::SEPARATOR
. - .lchmod ⇒ Object
- .lchown ⇒ Object
-
.link(old_name, new_name) ⇒ 0
Creates a new name for an existing file using a hard link.
-
.lstat(file_name) ⇒ Object
Same as
File::stat
, but does not follow the last symbolic link. -
.mtime(file_name) ⇒ Time
Returns the modification time for the named file as a Time object.
-
.owned?(file_name) ⇒ Boolean
Returns
true
if the named file exists and the effective used id of the calling process is the owner of the file. -
.pipe?(file_name) ⇒ Boolean
Returns
true
if the named file is a pipe. -
.readable?(file_name) ⇒ Boolean
Returns
true
if the named file is readable by the effective user id of this process. -
.readable_real?(file_name) ⇒ Boolean
Returns
true
if the named file is readable by the real user id of this process. -
.readlink(link_name) ⇒ Object
Returns the name of the file referenced by the given link.
-
.rename(old_name, new_name) ⇒ 0
Renames the given file to the new name.
-
.setgid?(file_name) ⇒ Boolean
Returns
true
if the named file has the setgid bit set. -
.setuid?(file_name) ⇒ Boolean
Returns
true
if the named file has the setuid bit set. -
.size(file_name) ⇒ Integer
Returns the size of
file_name
. -
.size?(file_name) ⇒ Integer?
Returns
nil
iffile_name
doesn’t exist or has zero size, the size of the file otherwise. -
.socket?(file_name) ⇒ Boolean
Returns
true
if the named file is a socket. -
.split(file_name) ⇒ Array
Splits the given string into a directory and a file component and returns them in a two-element array.
-
.stat(file_name) ⇒ Object
Returns a
File::Stat
object for the named file (seeFile::Stat
). -
.sticky?(file_name) ⇒ Boolean
Returns
true
if the named file has the sticky bit set. -
.symlink(old_name, new_name) ⇒ 0
Creates a symbolic link called new_name for the existing file old_name.
-
.symlink?(file_name) ⇒ Boolean
Returns
true
if the named file is a symbolic link. -
.truncate(file_name, integer) ⇒ 0
Truncates the file file_name to be at most integer bytes long.
-
.umask ⇒ Object
Returns the current umask value for this process.
-
.unlink ⇒ Object
Deletes the named files, returning the number of names passed as arguments.
- .utime ⇒ Object
-
.writable?(file_name) ⇒ Boolean
Returns
true
if the named file is writable by the effective user id of this process. -
.writable_real?(file_name) ⇒ Boolean
Returns
true
if the named file is writable by the real user id of this process. -
.zero?(file_name) ⇒ Boolean
Returns
true
if the named file exists and has a zero size.
Instance Method Summary collapse
-
#atime ⇒ Time
Returns the last access time (a
Time
object) for file, or epoch if file has not been accessed. -
#chmod(mode_int) ⇒ 0
Changes permission bits on file to the bit pattern represented by mode_int.
-
#chown(owner_int, group_int) ⇒ 0
Changes the owner and group of file to the given numeric owner and group id’s.
-
#ctime ⇒ Time
Returns the change time for file (that is, the time directory information about the file was changed, not the file itself).
-
#flock ⇒ Object
Locks or unlocks a file according to locking_constant (a logical or of the values in the table below).
-
#lstat ⇒ Object
Same as
IO#stat
, but does not follow the last symbolic link. -
#mtime ⇒ Time
Returns the modification time for file.
-
#path ⇒ Object
Returns the pathname used to create file as a string.
-
#truncate(integer) ⇒ 0
Truncates file to at most integer bytes.
Methods inherited from IO
Class Method Details
.atime(file_name) ⇒ Time
Returns the last access time for the named file as a Time object).
File.atime("testfile") #=> Wed Apr 09 08:51:48 CDT 2003
1569 1570 1571 |
# File 'file.c', line 1569 static VALUE rb_file_s_atime(klass, fname) VALUE klass, fname; |
.basename(file_name[, suffix]) ⇒ Object
Returns the last component of the filename given in file_name, which must be formed using forward slashes (“/
”) regardless of the separator used on the local file system. If suffix is given and present at the end of file_name, it is removed.
File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb"
File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"
2759 2760 2761 |
# File 'file.c', line 2759 static VALUE rb_file_s_basename(argc, argv) int argc; |
.blockdev?(file_name) ⇒ Boolean
Returns true
if the named file is a block device.
1056 1057 1058 |
# File 'file.c', line 1056 static VALUE test_b(obj, fname) VALUE obj, fname; |
.chardev?(file_name) ⇒ Boolean
Returns true
if the named file is a character device.
1084 1085 1086 |
# File 'file.c', line 1084 static VALUE test_c(obj, fname) VALUE obj, fname; |
.chmod(mode_int, file_name, ...) ⇒ Integer
Changes permission bits on the named file(s) to the bit pattern represented by mode_int. Actual effects are operating system dependent (see the beginning of this section). On Unix systems, see chmod(2)
for details. Returns the number of files processed.
File.chmod(0644, "testfile", "out") #=> 2
1721 1722 1723 |
# File 'file.c', line 1721 static VALUE rb_file_s_chmod(argc, argv) int argc; |
.chown(owner_int, group_int, file_name, ...) ⇒ Integer
Changes the owner and group of the named file(s) to the given numeric owner and group id’s. Only a process with superuser privileges may change the owner of a file. The current owner of a file may change the file’s group to any group to which the owner belongs. A nil
or -1 owner or group id is ignored. Returns the number of files processed.
File.chown(nil, 100, "testfile")
1853 1854 1855 |
# File 'file.c', line 1853 static VALUE rb_file_s_chown(argc, argv) int argc; |
.ctime(file_name) ⇒ Time
Returns the change time for the named file (the time at which directory information about the file was changed, not the file itself).
File.ctime("testfile") #=> Wed Apr 09 08:53:13 CDT 2003
1662 1663 1664 |
# File 'file.c', line 1662 static VALUE rb_file_s_ctime(klass, fname) VALUE klass, fname; |
.delete(file_name, ...) ⇒ Integer .unlink(file_name, ...) ⇒ Integer
Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error. See also Dir::rmdir
.
2210 2211 2212 |
# File 'file.c', line 2210 static VALUE rb_file_s_unlink(klass, args) VALUE klass, args; |
.directory?(file_name) ⇒ Boolean
Returns true
if the named file is a directory, false
otherwise.
File.directory?(".")
922 923 924 |
# File 'file.c', line 922 static VALUE test_d(obj, fname) VALUE obj, fname; |
.dirname(file_name) ⇒ Object
Returns all components of the filename given in file_name except the last one. The filename must be formed using forward slashes (“/
”) regardless of the separator used on the local file system.
File.dirname("/home/gumby/work/ruby.rb") #=> "/home/gumby/work"
2833 2834 2835 |
# File 'file.c', line 2833 static VALUE rb_file_s_dirname(klass, fname) VALUE klass, fname; |
.executable?(file_name) ⇒ Boolean
Returns true
if the named file is executable by the effective user id of this process.
1196 1197 1198 |
# File 'file.c', line 1196 static VALUE test_x(obj, fname) VALUE obj, fname; |
.executable_real?(file_name) ⇒ Boolean
Returns true
if the named file is executable by the real user id of this process.
1213 1214 1215 |
# File 'file.c', line 1213 static VALUE test_X(obj, fname) VALUE obj, fname; |
.exist?(file_name) ⇒ Boolean .exists?(file_name) ⇒ Boolean
Return true
if the named file exists.
1109 1110 1111 |
# File 'file.c', line 1109 static VALUE test_e(obj, fname) VALUE obj, fname; |
.exist?(file_name) ⇒ Boolean .exists?(file_name) ⇒ Boolean
Return true
if the named file exists.
1109 1110 1111 |
# File 'file.c', line 1109 static VALUE test_e(obj, fname) VALUE obj, fname; |
.expand_path(file_name[, dir_string]) ⇒ Object
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. The given pathname may start with a “~
”, which expands to the process owner’s home directory (the environment variable HOME
must be set correctly). “~
user” expands to the named user’s home directory.
File.("~oracle/bin") #=> "/home/oracle/bin"
File.("../../bin", "/tmp/x") #=> "/bin"
2707 2708 2709 |
# File 'file.c', line 2707 VALUE (argc, argv) int argc; |
.extname(path) ⇒ String
2886 2887 2888 |
# File 'file.c', line 2886 static VALUE rb_file_s_extname(klass, fname) VALUE klass, fname; |
.file?(file_name) ⇒ Boolean
Returns true
if the named file exists and is a regular file.
1234 1235 1236 |
# File 'file.c', line 1234 static VALUE test_f(obj, fname) VALUE obj, fname; |
.fnmatch(pattern, path, [flags]) ⇒ Boolean .fnmatch?(pattern, path, [flags]) ⇒ Boolean
Returns true if path matches against pattern The pattern is not a regular expression; instead it follows rules similar to shell filename globbing. It may contain the following metacharacters:
*
-
Matches any file. Can be restricted by other values in the glob.
*
will match all files;c*
will match all files beginning withc
;*c
will match all files ending withc
; andc
will match all files that havec
in them (including at the beginning or end). Equivalent to/ .* /x
in regexp. **
-
Matches directories recursively or files expansively.
?
-
Matches any one character. Equivalent to
/.{1}/
in regexp. [set]
-
Matches any one character in
set
. Behaves exactly like character sets in Regexp, including set negation ([^a-z]
). - <code></code>
-
Escapes the next metacharacter.
flags is a bitwise OR of the FNM_xxx
parameters. The same glob pattern and flags are used by Dir::glob
.
File.fnmatch('cat', 'cat') #=> true : match entire string
File.fnmatch('cat', 'category') #=> false : only match partial string
File.fnmatch('c{at,ub}s', 'cats') #=> false : { } isn't supported
File.fnmatch('c?t', 'cat') #=> true : '?' match only 1 character
File.fnmatch('c??t', 'cat') #=> false : ditto
File.fnmatch('c*', 'cats') #=> true : '*' match 0 or more characters
File.fnmatch('c*t', 'c/a/b/t') #=> true : ditto
File.fnmatch('ca[a-z]', 'cat') #=> true : inclusive bracket expression
File.fnmatch('ca[^t]', 'cat') #=> false : exclusive bracket expression ('^' or '!')
File.fnmatch('cat', 'CAT') #=> false : case sensitive
File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD) #=> true : case insensitive
File.fnmatch('?', '/', File::FNM_PATHNAME) #=> false : wildcard doesn't match '/' on FNM_PATHNAME
File.fnmatch('*', '/', File::FNM_PATHNAME) #=> false : ditto
File.fnmatch('[/]', '/', File::FNM_PATHNAME) #=> false : ditto
File.fnmatch('\?', '?') #=> true : escaped wildcard becomes ordinary
File.fnmatch('\a', 'a') #=> true : escaped ordinary remains ordinary
File.fnmatch('\a', '\a', File::FNM_NOESCAPE) #=> true : FNM_NOESACPE makes '\' ordinary
File.fnmatch('[\?]', '?') #=> true : can escape inside bracket expression
File.fnmatch('*', '.profile') #=> false : wildcard doesn't match leading
File.fnmatch('*', '.profile', File::FNM_DOTMATCH) #=> true period by default.
File.fnmatch('.*', '.profile') #=> true
rbfiles = '**' '/' '*.rb' # you don't have to do like this. just write in single string.
File.fnmatch(rbfiles, 'main.rb') #=> false
File.fnmatch(rbfiles, './main.rb') #=> false
File.fnmatch(rbfiles, 'lib/song.rb') #=> true
File.fnmatch('**.rb', 'main.rb') #=> true
File.fnmatch('**.rb', './main.rb') #=> false
File.fnmatch('**.rb', 'lib/song.rb') #=> true
File.fnmatch('*', 'dave/.profile') #=> true
pattern = '*' '/' '*'
File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME) #=> false
File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
pattern = '**' '/' 'foo'
File.fnmatch(pattern, 'a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, '/a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME) #=> false
File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
1918 1919 1920 |
# File 'dir.c', line 1918 static VALUE file_s_fnmatch(argc, argv, obj) int argc; |
.fnmatch(pattern, path, [flags]) ⇒ Boolean .fnmatch?(pattern, path, [flags]) ⇒ Boolean
Returns true if path matches against pattern The pattern is not a regular expression; instead it follows rules similar to shell filename globbing. It may contain the following metacharacters:
*
-
Matches any file. Can be restricted by other values in the glob.
*
will match all files;c*
will match all files beginning withc
;*c
will match all files ending withc
; andc
will match all files that havec
in them (including at the beginning or end). Equivalent to/ .* /x
in regexp. **
-
Matches directories recursively or files expansively.
?
-
Matches any one character. Equivalent to
/.{1}/
in regexp. [set]
-
Matches any one character in
set
. Behaves exactly like character sets in Regexp, including set negation ([^a-z]
). - <code></code>
-
Escapes the next metacharacter.
flags is a bitwise OR of the FNM_xxx
parameters. The same glob pattern and flags are used by Dir::glob
.
File.fnmatch('cat', 'cat') #=> true : match entire string
File.fnmatch('cat', 'category') #=> false : only match partial string
File.fnmatch('c{at,ub}s', 'cats') #=> false : { } isn't supported
File.fnmatch('c?t', 'cat') #=> true : '?' match only 1 character
File.fnmatch('c??t', 'cat') #=> false : ditto
File.fnmatch('c*', 'cats') #=> true : '*' match 0 or more characters
File.fnmatch('c*t', 'c/a/b/t') #=> true : ditto
File.fnmatch('ca[a-z]', 'cat') #=> true : inclusive bracket expression
File.fnmatch('ca[^t]', 'cat') #=> false : exclusive bracket expression ('^' or '!')
File.fnmatch('cat', 'CAT') #=> false : case sensitive
File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD) #=> true : case insensitive
File.fnmatch('?', '/', File::FNM_PATHNAME) #=> false : wildcard doesn't match '/' on FNM_PATHNAME
File.fnmatch('*', '/', File::FNM_PATHNAME) #=> false : ditto
File.fnmatch('[/]', '/', File::FNM_PATHNAME) #=> false : ditto
File.fnmatch('\?', '?') #=> true : escaped wildcard becomes ordinary
File.fnmatch('\a', 'a') #=> true : escaped ordinary remains ordinary
File.fnmatch('\a', '\a', File::FNM_NOESCAPE) #=> true : FNM_NOESACPE makes '\' ordinary
File.fnmatch('[\?]', '?') #=> true : can escape inside bracket expression
File.fnmatch('*', '.profile') #=> false : wildcard doesn't match leading
File.fnmatch('*', '.profile', File::FNM_DOTMATCH) #=> true period by default.
File.fnmatch('.*', '.profile') #=> true
rbfiles = '**' '/' '*.rb' # you don't have to do like this. just write in single string.
File.fnmatch(rbfiles, 'main.rb') #=> false
File.fnmatch(rbfiles, './main.rb') #=> false
File.fnmatch(rbfiles, 'lib/song.rb') #=> true
File.fnmatch('**.rb', 'main.rb') #=> true
File.fnmatch('**.rb', './main.rb') #=> false
File.fnmatch('**.rb', 'lib/song.rb') #=> true
File.fnmatch('*', 'dave/.profile') #=> true
pattern = '*' '/' '*'
File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME) #=> false
File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
pattern = '**' '/' 'foo'
File.fnmatch(pattern, 'a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, '/a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME) #=> false
File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
1918 1919 1920 |
# File 'dir.c', line 1918 static VALUE file_s_fnmatch(argc, argv, obj) int argc; |
.ftype(file_name) ⇒ String
1545 1546 1547 |
# File 'file.c', line 1545 static VALUE rb_file_s_ftype(klass, fname) VALUE klass, fname; |
.grpowned?(file_name) ⇒ Boolean
Returns true
if the named file exists and the effective group id of the calling process is the owner of the file. Returns false
on Windows.
1323 1324 1325 |
# File 'file.c', line 1323 static VALUE test_grpowned(obj, fname) VALUE obj, fname; |
.identical?(file_1, file_2) ⇒ Boolean
Returns true
if the named files are identical.
open("a", "w") {}
p File.identical?("a", "a") #=> true
p File.identical?("a", "./a") #=> true
File.link("a", "b")
p File.identical?("a", "b") #=> true
File.symlink("a", "c")
p File.identical?("a", "c") #=> true
open("d", "w") {}
p File.identical?("a", "d") #=> false
1422 1423 1424 |
# File 'file.c', line 1422 static VALUE test_identical(obj, fname1, fname2) VALUE obj, fname1, fname2; |
.join(string, ...) ⇒ Object
Returns a new string formed by joining the strings using File::SEPARATOR
.
File.join("usr", "mail", "gumby") #=> "usr/mail/gumby"
3014 3015 3016 |
# File 'file.c', line 3014 static VALUE rb_file_s_join(klass, args) VALUE klass, args; |
.lchmod ⇒ Object
1813 1814 1815 |
# File 'file.c', line 1813 static VALUE rb_file_s_lchmod(argc, argv) int argc; |
.lchown ⇒ Object
1971 1972 1973 |
# File 'file.c', line 1971 static VALUE rb_file_s_lchown(argc, argv) int argc; |
.link(old_name, new_name) ⇒ 0
2097 2098 2099 |
# File 'file.c', line 2097 static VALUE rb_file_s_link(klass, from, to) VALUE klass, from, to; |
.lstat(file_name) ⇒ Object
766 767 768 |
# File 'file.c', line 766 static VALUE rb_file_s_lstat(klass, fname) VALUE klass, fname; |
.mtime(file_name) ⇒ Time
Returns the modification time for the named file as a Time object.
File.mtime("testfile") #=> Tue Apr 08 12:58:04 CDT 2003
1615 1616 1617 |
# File 'file.c', line 1615 static VALUE rb_file_s_mtime(klass, fname) VALUE klass, fname; |
.owned?(file_name) ⇒ Boolean
Returns true
if the named file exists and the effective used id of the calling process is the owner of the file.
1292 1293 1294 |
# File 'file.c', line 1292 static VALUE test_owned(obj, fname) VALUE obj, fname; |
.pipe?(file_name) ⇒ Boolean
Returns true
if the named file is a pipe.
944 945 946 |
# File 'file.c', line 944 static VALUE test_p(obj, fname) VALUE obj, fname; |
.readable?(file_name) ⇒ Boolean
Returns true
if the named file is readable by the effective user id of this process.
1127 1128 1129 |
# File 'file.c', line 1127 static VALUE test_r(obj, fname) VALUE obj, fname; |
.readable_real?(file_name) ⇒ Boolean
Returns true
if the named file is readable by the real user id of this process.
1144 1145 1146 |
# File 'file.c', line 1144 static VALUE test_R(obj, fname) VALUE obj, fname; |
.readlink(link_name) ⇒ Object
2156 2157 2158 |
# File 'file.c', line 2156 static VALUE rb_file_s_readlink(klass, path) VALUE klass, path; |
.rename(old_name, new_name) ⇒ 0
Renames the given file to the new name. Raises a SystemCallError
if the file cannot be renamed.
File.rename("afile", "afile.bak") #=> 0
2231 2232 2233 |
# File 'file.c', line 2231 static VALUE rb_file_s_rename(klass, from, to) VALUE klass, from, to; |
.setgid?(file_name) ⇒ Boolean
Returns true
if the named file has the setgid bit set.
1376 1377 1378 |
# File 'file.c', line 1376 static VALUE test_sgid(obj, fname) VALUE obj, fname; |
.setuid?(file_name) ⇒ Boolean
Returns true
if the named file has the setuid bit set.
1358 1359 1360 |
# File 'file.c', line 1358 static VALUE test_suid(obj, fname) VALUE obj, fname; |
.size(file_name) ⇒ Integer
Returns the size of file_name
.
1477 1478 1479 |
# File 'file.c', line 1477 static VALUE rb_file_s_size(klass, fname) VALUE klass, fname; |
.size?(file_name) ⇒ Integer?
Returns nil
if file_name
doesn’t exist or has zero size, the size of the file otherwise.
1272 1273 1274 |
# File 'file.c', line 1272 static VALUE test_s(obj, fname) VALUE obj, fname; |
.socket?(file_name) ⇒ Boolean
Returns true
if the named file is a socket.
1013 1014 1015 |
# File 'file.c', line 1013 static VALUE test_S(obj, fname) VALUE obj, fname; |
.split(file_name) ⇒ Array
Splits the given string into a directory and a file component and returns them in a two-element array. See also File::dirname
and File::basename
.
File.split("/home/gumby/.profile") #=> ["/home/gumby", ".profile"]
2919 2920 2921 |
# File 'file.c', line 2919 static VALUE rb_file_s_split(klass, path) VALUE klass, path; |
.stat(file_name) ⇒ Object
Returns a File::Stat
object for the named file (see File::Stat
).
File.stat("testfile").mtime #=> Tue Apr 08 12:58:04 CDT 2003
710 711 712 |
# File 'file.c', line 710 static VALUE rb_file_s_stat(klass, fname) VALUE klass, fname; |
.sticky?(file_name) ⇒ Boolean
Returns true
if the named file has the sticky bit set.
1394 1395 1396 |
# File 'file.c', line 1394 static VALUE test_sticky(obj, fname) VALUE obj, fname; |
.symlink(old_name, new_name) ⇒ 0
Creates a symbolic link called new_name for the existing file old_name. Raises a NotImplemented
exception on platforms that do not support symbolic links.
File.symlink("testfile", "link2test") #=> 0
2127 2128 2129 |
# File 'file.c', line 2127 static VALUE rb_file_s_symlink(klass, from, to) VALUE klass, from, to; |
.symlink?(file_name) ⇒ Boolean
Returns true
if the named file is a symbolic link.
969 970 971 |
# File 'file.c', line 969 static VALUE test_l(obj, fname) VALUE obj, fname; |
.truncate(file_name, integer) ⇒ 0
3036 3037 3038 |
# File 'file.c', line 3036 static VALUE rb_file_s_truncate(klass, path, len) VALUE klass, path, len; |
.umask ⇒ Integer .umask(integer) ⇒ Integer
Returns the current umask value for this process. If the optional argument is given, set the umask to that value and return the previous value. Umask values are subtracted from the default permissions, so a umask of 0222
would make a file read-only for everyone.
File.umask(0006) #=> 18
File.umask #=> 6
2284 2285 2286 |
# File 'file.c', line 2284 static VALUE rb_file_s_umask(argc, argv) int argc; |
.delete(file_name, ...) ⇒ Integer .unlink(file_name, ...) ⇒ Integer
Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error. See also Dir::rmdir
.
2210 2211 2212 |
# File 'file.c', line 2210 static VALUE rb_file_s_unlink(klass, args) VALUE klass, args; |
.utime ⇒ Object
2045 2046 2047 |
# File 'file.c', line 2045 static VALUE rb_file_s_utime(argc, argv) int argc; |
.writable?(file_name) ⇒ Boolean
Returns true
if the named file is writable by the effective user id of this process.
1162 1163 1164 |
# File 'file.c', line 1162 static VALUE test_w(obj, fname) VALUE obj, fname; |
.writable_real?(file_name) ⇒ Boolean
Returns true
if the named file is writable by the real user id of this process.
1179 1180 1181 |
# File 'file.c', line 1179 static VALUE test_W(obj, fname) VALUE obj, fname; |
.zero?(file_name) ⇒ Boolean
Returns true
if the named file exists and has a zero size.
1253 1254 1255 |
# File 'file.c', line 1253 static VALUE test_z(obj, fname) VALUE obj, fname; |
Instance Method Details
#atime ⇒ Time
Returns the last access time (a Time
object)
for <i>file</i>, or epoch if <i>file</i> has not been accessed.
File.new("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
1591 1592 1593 |
# File 'file.c', line 1591 static VALUE rb_file_atime(obj) VALUE obj; |
#chmod(mode_int) ⇒ 0
1752 1753 1754 |
# File 'file.c', line 1752 static VALUE rb_file_chmod(obj, vmode) VALUE obj, vmode; |
#chown(owner_int, group_int) ⇒ 0
Changes the owner and group of file to the given numeric owner and group id’s. Only a process with superuser privileges may change the owner of a file. The current owner of a file may change the file’s group to any group to which the owner belongs. A nil
or -1 owner or group id is ignored. Follows symbolic links. See also File#lchown
.
File.new("testfile").chown(502, 1000)
1896 1897 1898 |
# File 'file.c', line 1896 static VALUE rb_file_chown(obj, owner, group) VALUE obj, owner, group; |
#flock ⇒ Object
Locks or unlocks a file according to locking_constant (a logical or of the values in the table below). Returns false
if File::LOCK_NB
is specified and the operation would otherwise have blocked. Not available on all platforms.
Locking constants (in class File):
LOCK_EX | Exclusive lock. Only one process may hold an
| exclusive lock for a given file at a time.
----------+------------------------------------------------
LOCK_NB | Don't block when locking. May be combined
| with other lock options using logical or.
----------+------------------------------------------------
LOCK_SH | Shared lock. Multiple processes may each hold a
| shared lock for a given file at the same time.
----------+------------------------------------------------
LOCK_UN | Unlock.
Example:
File.new("testfile").flock(File::LOCK_UN) #=> 0
3213 3214 3215 |
# File 'file.c', line 3213 static VALUE rb_file_flock(obj, operation) VALUE obj; |
#mtime ⇒ Time
1636 1637 1638 |
# File 'file.c', line 1636 static VALUE rb_file_mtime(obj) VALUE obj; |
#truncate(integer) ⇒ 0
3090 3091 3092 |
# File 'file.c', line 3090 static VALUE rb_file_truncate(obj, len) VALUE obj, len; |