End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
String#<=>
string <=> other_string → -1, 0, 1, or nil
string.c
View on GitHub ↗
static VALUE
rb_str_cmp_m(VALUE str1, VALUE str2)
{
int result;
VALUE s = rb_check_string_type(str2);
if (NIL_P(s)) {
return rb_invcmp(str1, str2);
}
result = rb_str_cmp(str1, s);
return INT2FIX(result);
}
Compares self and other_string, 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' <=> 'foo' # => 0 'foo' <=> 'food' # => -1 'food' <=> 'foo' # => 1 'FOO' <=> 'foo' # => -1 'foo' <=> 'FOO' # => 1 'foo' <=> 1 # => nil