Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
File#mtime
() -> ::Time
mtime → time
file.c
View on GitHub ↗
static VALUE
rb_file_mtime(VALUE obj)
{
rb_io_t *fptr;
struct stat st;
GetOpenFile(obj, fptr);
if (fstat(fptr->fd, &st) == -1) {
rb_sys_fail_path(fptr->pathv);
}
return stat_time(stat_mtimespec(&st));
}
Returns a new Time object containing the modification time for self; see Modification Time:
Example
path = 't.tmp' file = File.new(path, 'w+') file.mtime # => 2026-09-19 08:41:29.357110007 -0500 file.write('foo') file.flush file.mtime # => 2026-09-19 08:41:46.321965574 -0500 File.unlink(path)