Skip to content

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

Compares the snapshots in self and other, by comparing their modification times self.mtime and other.mtime.

Returns:

  • -1, if self.mtime is earlier.

  • 0, if the two values are equal.

  • 1, if self.mtime is later.

  • nil, if other is 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.

See also .new #atime #birthtime also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0