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 → -1Time.
File.stat("testfile").atime → Wed Dec 31 18:00:00 CST 1969File.stat("testfile").blksize → 4096true or falsetrue 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? → falseFile.stat("testfile").blocks → 2true or falsetrue 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? → trueFile.stat("testfile").ctime → Sun Jun 09 00:17:19 CDT 2002File.stat("testfile").dev → 774true or falsetrue if statfile is a directory, false
otherwise.
File.stat("testfile").directory? → false
File.stat(".").directory? → truetrue or falsetrue 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? → falsetrue or falseexecutable?, but tests using the real owner of the
process.
true or falsetrue if statfile is a regular file (not a device
file, pipe, socket, etc.).
File.stat("testfile").file? → truefile”,
“directory”,
“characterSpecial”,
“blockSpecial”,
“fifo”,
“link”, or
“socket”.
File.stat("/dev/tty").ftype → "characterSpecial"File.stat("testfile").gid → 500true or falsefalse.
File.stat("testfile").grpowned? → true
File.stat("/etc/passwd").grpowned? → falseFile.stat("testfile").ino → 43331stat(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 2002File.stat("testfile").nlink → 1
File.link("testfile", "testfile.bak") → 0
File.stat("testfile").nlink → 2true or falsetrue 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? → falsetrue or falsetrue 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 → 512true or falsetrue if statfile is readable by the effective
user id of this process.
File.stat("testfile").readable? → truetrue or falsetrue if statfile is readable by the real
user id of this process.
File.stat("testfile").readable_real? → truetrue or falsetrue 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? → truetrue or falsetrue 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? → trueFile.stat("testfile").size → 66nilnil if statfile is a zero-length file; otherwise,
returns the file size.
File.stat("testfile").size? → 66true or falsetrue if statfile is a socket, false if
it isn't or
if the operating system doesn't support this feature.
File.stat("testfile").socket? → falsetrue or falsetrue 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? → falsetrue or falsetrue 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? → trueFile.stat("testfile").uid → 501true or falsetrue if statfile is writable by the effective
user id of this process.
File.stat("testfile").writable? → truetrue or falsetrue if statfile is writable by the real
user id of this process.
File.stat("testfile").writable_real? → truetrue or falsetrue if statfile is a zero-length file; false
otherwise.
File.stat("testfile").zero? → falseExtracted 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.