Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
File.stat
(::path file_name) -> ::File::Stat
File.stat(path) → stat
file.c
View on GitHub ↗
static VALUE
rb_file_s_stat(VALUE klass, VALUE fname)
{
rb_io_stat_data st;
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
rb_sys_fail_path(fname);
}
return rb_statx_new(&st);
}
Returns a new File::Stat object for the entry at path. Unlike File.lstat, does follow symbolic links; therefore if the entry is a symbolic link, the returned object contains information for the target entry, not the symbolic link:
Example
filepath = '/etc/passwd' linkpath = '/tmp/foo' File.symlink(filepath, linkpath) # Method File.stat follows the symlink, so the birthtimes are the same. File.stat(filepath).birthtime # => 2025-06-10 11:10:47.358999941 -0500 File.stat(linkpath).birthtime # => 2025-06-10 11:10:47.358999941 -0500 # Method File.lstat does not follow the symlink, so the birthtimes are different. File.lstat(filepath).birthtime # => 2025-06-10 11:10:47.358999941 -0500 File.lstat(linkpath).birthtime # => 2026-09-23 14:01:18.248754979 -0500 File.delete(linkpath) # Clean up.