class File::Stat
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.
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2526
def initialize: (String file) -> void
Create a File::Stat object for the given file name (raising an exception if the file doesn’t exist).
Public Instance Methods
(File::Stat other) → Integer
(untyped) → nil
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2556
def <=>: (File::Stat other) -> Integer
| (untyped) -> nil
Compares self and other, by comparing their modification times; that is, by comparing self.mtime and other.mtime.
Returns:
-
-1, ifself.mtimeis earlier. -
0, if the two values are equal. -
1, ifself.mtimeis later. -
nil, ifotheris not aFile::Statobject.
Examples:
stat0 = File.stat('README.md') stat1 = File.stat('NEWS.md') stat0.mtime # => 2025-12-20 15:33:05.6972341 -0600 stat1.mtime # => 2025-12-20 16:02:08.2672945 -0600 stat0 <=> stat1 # => -1 stat0 <=> stat0.dup # => 0 stat1 <=> stat0 # => 1 stat0 <=> :foo # => nil
Class File::Stat includes module Comparable, each of whose methods uses File::Stat#<=> for comparison.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2567
def atime: () -> Time
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
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2589
def birthtime: () -> Time
Returns the birth time for stat.
If the platform doesn’t have birthtime, raises NotImplementedError.
File.write("testfile", "foo") sleep 10 File.write("testfile", "bar") sleep 10 File.chmod(0644, "testfile") sleep 10 File.read("testfile") File.stat("testfile").birthtime #=> 2014-02-24 11:19:17 +0900 File.stat("testfile").mtime #=> 2014-02-24 11:19:27 +0900 File.stat("testfile").ctime #=> 2014-02-24 11:19:37 +0900 File.stat("testfile").atime #=> 2014-02-24 11:19:47 +0900
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2600
def blksize: () -> Integer?
Returns the native file system’s block size. Will return nil on platforms that don’t support this information.
File.stat("testfile").blksize #=> 4096
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2612
def blockdev?: () -> bool
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 File.stat("/dev/hda1").blockdev? #=> true
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2623
def blocks: () -> Integer?
Returns the number of native file system blocks allocated for this file, or nil if the operating system doesn’t support this feature.
File.stat("testfile").blocks #=> 2
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2634
def chardev?: () -> bool
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
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2647
def ctime: () -> Time
Returns the change time for stat (that is, the time directory information about the file was changed, not the file itself).
Note that on Windows (NTFS), returns creation time (birth time).
File.stat("testfile").ctime #=> Wed Apr 09 08:53:14 CDT 2003
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2657
def dev: () -> Integer
Returns an integer representing the device on which stat resides.
File.stat("testfile").dev #=> 774
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2668
def dev_major: () -> Integer
Returns the major part of File_Stat#dev or nil.
File.stat("/dev/fd1").dev_major #=> 2 File.stat("/dev/tty").dev_major #=> 5
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2679
def dev_minor: () -> Integer
Returns the minor part of File_Stat#dev or nil.
File.stat("/dev/fd1").dev_minor #=> 1 File.stat("/dev/tty").dev_minor #=> 0
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2690
def directory?: () -> bool
Returns true if stat is a directory, false otherwise.
File.stat("testfile").directory? #=> false File.stat(".").directory? #=> true
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2702
def executable?: () -> bool
Returns true if stat 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
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2711
def executable_real?: () -> bool
Same as executable?, but tests using the real owner of the process.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2722
def file?: () -> bool
Returns true if stat is a regular file (not a device file, pipe, socket, etc.).
File.stat("testfile").file? #=> true
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2734
def ftype: () -> String
Identifies the type of stat. The return string is one of: file`'', directory'', ```characterSpecial”, blockSpecial`'', fifo'', ```link”, socket`'', or unknown`”.
File.stat("/dev/tty").ftype #=> "characterSpecial"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2744
def gid: () -> Integer
Returns the numeric group id of the owner of stat.
File.stat("testfile").gid #=> 500
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2756
def grpowned?: () -> bool
Returns true if the effective group id of the process is the same as the group id of stat. On Windows, returns false.
File.stat("testfile").grpowned? #=> true File.stat("/etc/passwd").grpowned? #=> false
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2766
def ino: () -> Integer
Returns the inode number for stat.
File.stat("testfile").ino #=> 1083669
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2782
def inspect: () -> String
Produce a nicely formatted description of stat.
File.stat("/etc/passwd").inspect #=> "#<File::Stat dev=0xe000005, ino=1078078, mode=0100644, # nlink=1, uid=0, gid=0, rdev=0x0, size=1374, blksize=4096, # blocks=8, atime=Wed Dec 10 10:16:12 CST 2003, # mtime=Fri Sep 12 15:41:41 CDT 2003, # ctime=Mon Oct 27 11:20:27 CST 2003, # birthtime=Mon Aug 04 08:13:49 CDT 2003>"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2795
def mode: () -> Integer
Returns an integer representing the permission bits of stat. 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"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2805
def mtime: () -> Time
Returns the modification time of stat.
File.stat("testfile").mtime #=> Wed Apr 09 08:53:14 CDT 2003
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2817
def nlink: () -> Integer
Returns the number of hard links to stat.
File.stat("testfile").nlink #=> 1 File.link("testfile", "testfile.bak") #=> 0 File.stat("testfile").nlink #=> 2
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2829
def owned?: () -> bool
Returns true if the effective user id of the process is the same as the owner of stat.
File.stat("testfile").owned? #=> true File.stat("/etc/passwd").owned? #=> false
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2838
def pipe?: () -> bool
Returns true if the operating system supports pipes and stat is a pipe; false otherwise.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2850
def rdev: () -> Integer?
Returns an integer representing the device type on which stat resides. Returns nil if the operating system doesn’t support this feature.
File.stat("/dev/fd1").rdev #=> 513 File.stat("/dev/tty").rdev #=> 1280
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2861
def rdev_major: () -> Integer
Returns the major part of File_Stat#rdev or nil.
File.stat("/dev/fd1").rdev_major #=> 2 File.stat("/dev/tty").rdev_major #=> 5
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2872
def rdev_minor: () -> Integer
Returns the minor part of File_Stat#rdev or nil.
File.stat("/dev/fd1").rdev_minor #=> 1 File.stat("/dev/tty").rdev_minor #=> 0
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2882
def readable?: () -> bool
Returns true if stat is readable by the effective user id of this process.
File.stat("testfile").readable? #=> true
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2892
def readable_real?: () -> bool
Returns true if stat is readable by the real user id of this process.
File.stat("testfile").readable_real? #=> true
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2903
def setgid?: () -> bool
Returns true if stat 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
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2914
def setuid?: () -> bool
Returns true if stat 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 vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2924
def size: () -> Integer
Returns the size of stat in bytes.
File.stat("testfile").size #=> 66
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2935
def size?: () -> Integer?
Returns nil if stat is a zero-length file, the size of the file otherwise.
File.stat("testfile").size? #=> 66 File.stat(File::NULL).size? #=> nil
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2946
def socket?: () -> bool
Returns true if stat is a socket, false if it isn’t or if the operating system doesn’t support this feature.
File.stat("testfile").socket? #=> false
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2957
def sticky?: () -> bool
Returns true if stat 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
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2972
def symlink?: () -> bool
Returns true if stat 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 vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2982
def uid: () -> Integer
Returns the numeric user id of the owner of stat.
File.stat("testfile").uid #=> 501
() → Integer?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 2995
def world_readable?: () -> Integer?
If stat is readable by others, returns an integer representing the file permission bits of stat. Returns nil otherwise. The meaning of the bits is platform dependent; on Unix systems, see stat(2).
m = File.stat("/etc/passwd").world_readable? #=> 420 sprintf("%o", m) #=> "644"
() → Integer?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 3008
def world_writable?: () -> Integer?
If stat is writable by others, returns an integer representing the file permission bits of stat. Returns nil otherwise. The meaning of the bits is platform dependent; on Unix systems, see stat(2).
m = File.stat("/tmp").world_writable? #=> 511 sprintf("%o", m) #=> "777"
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 3018
def writable?: () -> bool
Returns true if stat is writable by the effective user id of this process.
File.stat("testfile").writable? #=> true
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 3028
def writable_real?: () -> bool
Returns true if stat is writable by the real user id of this process.
File.stat("testfile").writable_real? #=> true
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/file.rbs, line 3038
def zero?: () -> bool
Returns true if stat is a zero-length file; false otherwise.
File.stat("testfile").zero? #=> false