Programming Ruby

The Pragmatic Programmer's Guide

Previous < Contents ^
Next >
class File::Stat
Parent: Object
Version: 1.6

Index:

<=> atime blksize blockdev? blocks chardev? ctime dev directory? executable? executable_real? file? ftype gid grpowned? ino mode mtime nlink owned? pipe? rdev readable? readable_real? setgid? setuid? size size? socket? sticky? symlink? uid writable? writable_real? zero?


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 on page 425.
mixins
Comparable: <, <=, ==, >=, >, between?

instance methods
<=> statfile <=> anOtherStat -> -1, 0, 1

Compares 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

atime statfile.atime -> aTime

Returns the last access time for this file as an object of class Time.

File.stat("testfile").atime Wed Dec 31 18:00:00 CST 1969

blksize statfile.blksize -> anInteger

Returns the native file system's block size. Will return 0 on platforms that don't support this information.

File.stat("testfile").blksize 4096

blockdev? statfile.blockdev? -> true or false

Returns 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

blocks statfile.blocks -> anInteger

Returns the number of native file system blocks allocated for this file, or 0 if the operating system doesn't support this feature.

File.stat("testfile").blocks 2

chardev? statfile.chardev? -> true or false

Returns 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

ctime statfile.ctime -> aTime

Returns the change time for statfile (that is, the time directory information about the file was changed, not the file itself).

File.stat("testfile").ctime Sun Jun 09 00:17:19 CDT 2002

dev statfile.dev -> aFixnum

Returns an integer representing the device on which statfile resides.

File.stat("testfile").dev 774

directory? statfile.directory? -> true or false

Returns true if statfile is a directory, false otherwise.

File.stat("testfile").directory? false
File.stat(".").directory? true

executable? statfile.executable? -> true or false

Returns 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

executable_real? statfile.executable_real? -> true or false

Same as executable?, but tests using the real owner of the process.

file? statfile.file? -> true or false

Returns true if statfile is a regular file (not a device file, pipe, socket, etc.).

File.stat("testfile").file? true

ftype statfile.ftype -> fileType

Identifies the type of statfile. The return string is one of: ``file'', ``directory'', ``characterSpecial'', ``blockSpecial'', ``fifo'', ``link'', or ``socket''.

File.stat("/dev/tty").ftype "characterSpecial"

gid statfile.gid -> aFixnum

Returns the numeric group id of the owner of statfile.

File.stat("testfile").gid 500

grpowned? statfile.grpowned? -> true or false

Returns true if the effective group id of the process is the same as the group id of statfile. On Windows NT, returns false.

File.stat("testfile").grpowned? true
File.stat("/etc/passwd").grpowned? false

ino statfile.ino -> aFixnum

Returns the inode number for statfile.

File.stat("testfile").ino 43331

mode statfile.mode -> aFixnum

Returns an integer representing the permission bits of statfile. The meaning of the bits is platform dependent; on Unix systems, see stat(2).

File.chmod(0644, "testfile") 1
s = File.stat("testfile")
sprintf("%o", s.mode) "100644"

mtime statfile.mtime -> aTime

Returns the modification time for statfile.

File.stat("testfile").mtime Sun Jun 09 00:17:19 CDT 2002

nlink statfile.nlink -> aFixnum

Returns the number of hard links to statfile.

File.stat("testfile").nlink 1
File.link("testfile", "testfile.bak") 0
File.stat("testfile").nlink 2

owned? statfile.owned? -> true or false

Returns 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

pipe? statfile.pipe? -> true or false

Returns true if the operating system supports pipes and statfile is a pipe; false otherwise.

rdev statfile.rdev -> aFixnum

Returns an integer representing the device type on which statfile resides. Returns 0 if the operating system doesn't support this feature.

File.stat("/dev/fd0").rdev 512

readable? statfile.readable? -> true or false

Returns true if statfile is readable by the effective user id of this process.

File.stat("testfile").readable? true

readable_real? statfile.readable_real? -> true or false

Returns true if statfile is readable by the real user id of this process.

File.stat("testfile").readable_real? true

setgid? statfile.setgid? -> true or false

Returns 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

setuid? statfile.setuid? -> true or false

Returns 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

size statfile.size -> aFixnum

Returns the size of statfile in bytes.

File.stat("testfile").size 66

size? statfile.size? -> aFixnum or nil

Returns nil if statfile is a zero-length file; otherwise, returns the file size.

File.stat("testfile").size? 66

socket? statfile.socket? -> true or false

Returns 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

sticky? statfile.sticky? -> true or false

Returns 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

symlink? statfile.symlink? -> true or false

Returns 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

uid statfile.uid -> aFixnum

Returns the numeric user id of the owner of statfile.

File.stat("testfile").uid 501

writable? statfile.writable? -> true or false

Returns true if statfile is writable by the effective user id of this process.

File.stat("testfile").writable? true

writable_real? statfile.writable_real? -> true or false

Returns true if statfile is writable by the real user id of this process.

File.stat("testfile").writable_real? true

zero? statfile.zero? -> true or false

Returns true if statfile is a zero-length file; false otherwise.

File.stat("testfile").zero? false


Previous < Contents ^
Next >

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.