Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
File::Stat#<=>
(self other) -> (-1 | 0 | 1)
(untyped) -> (-1 | 0 | 1)?
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 the snapshots in self and other, by comparing their modification times self.mtime and other.mtime.
Returns:
-
-1, ifself.mtimeis earlier. -
0, if the two values are equal. -
1, ifself.mtimeis later. -
nil, ifotheris not a File::Stat object.
Examples:
Example
stat0 = File.stat('/etc') stat1 = File.stat('/tmp') stat0.mtime # => 2026-10-02 07:52:03.151556038 -0500 stat1.mtime # => 2026-10-03 12:22:25.719215899 -0500 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.