End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
String#casecmp
str.casecmp(other_str) → -1, 0, 1, or nil
string.c
View on GitHub ↗
static VALUE
rb_str_casecmp(VALUE str1, VALUE str2)
{
VALUE s = rb_check_string_type(str2);
if (NIL_P(s)) {
return Qnil;
}
return str_casecmp(str1, s);
}
Compares self and other_string, ignoring case, and returning:
-
-1 if
other_stringis larger. -
0 if the two are equal.
-
1 if
other_stringis smaller. -
nilif 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