Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Symbol#casecmp

sym.casecmp(other_symbol) → -1, 0, +1, or nil

Case-insensitive version of Symbol#<=>. Currently, case-insensitivity only works on characters A-Z/a-z, not all of Unicode. This is different from Symbol#casecmp?.

Example
:aBcDeF.casecmp(:abcde)     #=> 1
:aBcDeF.casecmp(:abcdef)    #=> 0
:aBcDeF.casecmp(:abcdefg)   #=> -1
:abcdef.casecmp(:ABCDEF)    #=> 0

nil is returned if the two symbols have incompatible encodings, or if other_symbol is not a symbol.

Example
:foo.casecmp(2)   #=> nil
"\u{e4 f6 fc}".encode("ISO-8859-1").to_sym.casecmp(:"\u{c4 d6 dc}")   #=> nil
See also #casecmp? #capitalize #as_json #downcase also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1