Skip to content
instance method

String#<=>

(::string) -> (-1 | 0 | 1)
(untyped) -> (-1 | 0 | 1)?
self <=> other → -1, 0, 1, or nil

Compares self and other, evaluating their contents, not their lengths.

Returns:

  • -1, if self is smaller.

  • 0, if the two are equal.

  • 1, if self is larger.

  • nil, if the two are incomparable.

Examples:

Example
'a'  <=> 'b'  # => -1
'a'  <=> 'ab' # => -1
'a'  <=> 'a'  # => 0
'b'  <=> 'a'  # => 1
'ab' <=> 'a'  # => 1
'a'  <=> :a   # => nil

Class String includes module Comparable, each of whose methods uses String#<=> for comparison.

Related: see Comparing.

See also #<< #== #-@ #=== also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0