Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

String#casecmp

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

Compares self.downcase and other_string.downcase; returns:

  • -1 if other_string.downcase is larger.

  • 0 if the two are equal.

  • 1 if other_string.downcase is smaller.

  • nil if the two are incomparable.

Examples:

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

See Case Mapping.

Related: String#casecmp?.

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