Skip to content
instance method

String#casecmp

(::string other) -> (-1 | 0 | 1)
(untyped) -> (-1 | 0 | 1)?
casecmp(other_string) → -1, 0, 1, or nil

Ignoring case, compares self and other_string; returns:

  • -1 if self.downcase is smaller than other_string.downcase.

  • 0 if the two are equal.

  • 1 if self.downcase is larger than other_string.downcase.

  • nil if the two are incomparable.

See Case Mapping.

Examples:

Example
'foo'.casecmp('goo')  # => -1
'goo'.casecmp('foo')  # => 1
'foo'.casecmp('food') # => -1
'food'.casecmp('foo') # => 1
'FOO'.casecmp('foo')  # => 0
'foo'.casecmp('FOO')  # => 0
'foo'.casecmp(1)      # => nil

Related: see Comparing.

See also #casecmp? #capitalize! #capitalize #center also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0