instance method
File::Stat#<=>
(::File::Stat other) -> ::Integer
(untyped) -> nil
self <=> other → -1, 0, 1, or nil
file.c
View on GitHub ↗
static VALUE
rb_stat_cmp(VALUE self, VALUE other)
{
if (rb_obj_is_kind_of(other, rb_obj_class(self))) {
stat_timestamp ts1 = statx_mtimespec(get_stat(self));
stat_timestamp ts2 = statx_mtimespec(get_stat(other));
if (ts1.tv_sec == ts2.tv_sec) {
if (ts1.tv_nsec == ts2.tv_nsec) return INT2FIX(0);
if (ts1.tv_nsec < ts2.tv_nsec) return INT2FIX(-1);
return INT2FIX(1);
}
if (ts1.tv_sec < ts2.tv_sec) return INT2FIX(-1);
return INT2FIX(1);
}
return Qnil;
}
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:
Example
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.