Objects of class File::Stat
encapsulate common status information for File
objects. The information is recorded at the moment the File::Stat
object is created; changes made to the file after that point will not be reflected.
File::Stat
objects are returned by IO#stat
, File.stat
, File#lstat
, and File.lstat
. Many of these methods return platform-specific values, and not all values are meaningful on all systems.
See also Kernel#test
.
Comparable
<
, <=
, ==
, >=
, >
, between?
File::Stat
objects by comparing their respective
modification times.
f1 = File.new("f1", "w")
sleep 1
f2 = File.new("f2", "w")
f1.stat <=> f2.stat → -1
Time
.
File.stat("testfile").atime → Wed Dec 31 18:00:00 CST 1969
File.stat("testfile").blksize → 4096
true
or false
true
if the file is a block device, false
if it isn't or
if the operating system doesn't support this feature.
File.stat("testfile").blockdev? → false
File.stat("testfile").blocks → 2
true
or false
true
if the file is a character device, false
if it isn't or
if the operating system doesn't support this feature.
File.stat("/dev/tty").chardev? → true
File.stat("testfile").ctime → Sun Jun 09 00:17:19 CDT 2002
File.stat("testfile").dev → 774
true
or false
true
if statfile is a directory, false
otherwise.
File.stat("testfile").directory? → false
File.stat(".").directory? → true
true
or false
true
if statfile is executable or if the
operating system doesn't distinguish executable files from
nonexecutable files. The tests are made using the effective
owner of the process.
File.stat("testfile").executable? → false
true
or false
executable?
, but tests using the real owner of the
process.
true
or false
true
if statfile is a regular file (not a device
file, pipe, socket, etc.).
File.stat("testfile").file? → true
file
”,
“directory
”,
“characterSpecial
”,
“blockSpecial
”,
“fifo
”,
“link
”, or
“socket
”.
File.stat("/dev/tty").ftype → "characterSpecial"
File.stat("testfile").gid → 500
true
or false
false
.
File.stat("testfile").grpowned? → true
File.stat("/etc/passwd").grpowned? → false
File.stat("testfile").ino → 43331
stat(2)
.
File.chmod(0644, "testfile") → 1
s = File.stat("testfile")
sprintf("%o", s.mode) → "100644"
File.stat("testfile").mtime → Sun Jun 09 00:17:19 CDT 2002
File.stat("testfile").nlink → 1
File.link("testfile", "testfile.bak") → 0
File.stat("testfile").nlink → 2
true
or false
true
if the effective user id of the process is the same
as the owner of statfile.
File.stat("testfile").owned? → true
File.stat("/etc/passwd").owned? → false
true
or false
true
if the operating system supports pipes and
statfile is a pipe; false
otherwise.
0
if the operating system doesn't support this feature.
File.stat("/dev/fd0").rdev → 512
true
or false
true
if statfile is readable by the effective
user id of this process.
File.stat("testfile").readable? → true
true
or false
true
if statfile is readable by the real
user id of this process.
File.stat("testfile").readable_real? → true
true
or false
true
if statfile has the set-group-id
permission bit set, false
if it doesn't or
if the operating system doesn't support this feature.
File.stat("/usr/sbin/lpc").setgid? → true
true
or false
true
if statfile has the set-user-id
permission bit set, false
if it doesn't or
if the operating system doesn't support this feature.
File.stat("/bin/su").setuid? → true
File.stat("testfile").size → 66
nil
nil
if statfile is a zero-length file; otherwise,
returns the file size.
File.stat("testfile").size? → 66
true
or false
true
if statfile is a socket, false
if
it isn't or
if the operating system doesn't support this feature.
File.stat("testfile").socket? → false
true
or false
true
if statfile has its sticky bit set,
false
if it doesn't or if the operating system doesn't
support this feature.
File.stat("testfile").sticky? → false
true
or false
true
if statfile is a symbolic link,
false
if it isn't or if the operating system doesn't
support this feature. As File.stat
automatically follows
symbolic links, symlink?
will always be
false
for an object returned by File.stat
.
File.symlink("testfile", "alink") → 0
File.stat("alink").symlink? → false
File.lstat("alink").symlink? → true
File.stat("testfile").uid → 501
true
or false
true
if statfile is writable by the effective
user id of this process.
File.stat("testfile").writable? → true
true
or false
true
if statfile is writable by the real
user id of this process.
File.stat("testfile").writable_real? → true
true
or false
true
if statfile is a zero-length file; false
otherwise.
File.stat("testfile").zero? → false
Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.