Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Pathname#<=>

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

Compares the contents of self and other as strings; see String#<=>.

Returns:

  • -1 if self‘s string is smaller than other’s string.

  • 0 if the two are equal.

  • 1 if self‘s string is larger than other’s string.

  • nil if other is not a Pathname.

Examples:

Example
Pathname('a')  <=> Pathname('b')  # => -1
Pathname('a')  <=> Pathname('ab') # => -1
Pathname('a')  <=> Pathname('a')  # => 0
Pathname('b')  <=> Pathname('a')  # => 1
Pathname('ab') <=> Pathname('a')  # => 1
Pathname('ab') <=> 'a'            # => nil

Two pathnames that are different may refer to the same entry in the filesystem:

Example
Pathname('lib') <=> Pathname('./lib') # => 1
See also #/ #== #+ #=== also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0