Method: String#<=>

Defined in:
string.c

#<=>(other_string) ⇒ -1, ...

Compares self and other_string, returning:

  • -1 if other_string is smaller.

  • 0 if the two are equal.

  • 1 if other_string is larger.

  • nil if the two are incomparable.

Examples:

'foo' <=> 'foo' # => 0
'foo' <=> 'food' # => -1
'food' <=> 'foo' # => 1
'FOO' <=> 'foo' # => -1
'foo' <=> 'FOO' # => 1
'foo' <=> 1 # => nil

Returns:

  • (-1, 0, 1, nil)


3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
# File 'string.c', line 3451

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);
}