Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
File#lstat
() -> (::File::Stat | nil)
lstat → stat
file.c
View on GitHub ↗
static VALUE
rb_file_lstat(VALUE obj)
{
#ifdef HAVE_LSTAT
rb_io_t *fptr;
rb_io_stat_data st;
VALUE path;
GetOpenFile(obj, fptr);
if (NIL_P(fptr->pathv)) return Qnil;
path = rb_str_encode_ospath(fptr->pathv);
if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
rb_sys_fail_path(fptr->pathv);
}
return rb_statx_new(&st);
#else
return rb_io_stat(obj);
#endif
}
Returns a new File::Stat object for self. Unlike File#stat, does not follow symbolic links; therefore if self is a symbolic link, the returned object contains information for self, not the target entry:
Example
file_path = '/etc/passwd' link_path = '/tmp/foo' File.symlink(file_path, link_path) file = File.new(file_path) # => #<File:/etc/passwd> link = File.new(link_path) # => #<File:/tmp/foo> # Method lstat does not follow the symlink, so the birthtimes are different. file.lstat.birthtime # => 2025-06-10 11:10:47.358999941 -0500 link.lstat.birthtime # => 2026-09-23 16:35:59.321566598 -0500 # Method stat does follow the symlink, so the birthtimes are the same. file.stat.birthtime # => 2025-06-10 11:10:47.358999941 -0500 link.stat.birthtime # => 2025-06-10 11:10:47.358999941 -0500 File.delete(link_path) # Clean up.