Skip to content
instance method

File::Stat#<=>

(::File::Stat other) -> ::Integer
(untyped) -> nil
self <=> other → -1, 0, 1, or nil

Compares self and other, by comparing their modification times; that is, by comparing 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('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.

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