instance method
String#<=>
(::string) -> (-1 | 0 | 1)
(untyped) -> (-1 | 0 | 1)?
self <=> other → -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, evaluating their contents, not their lengths.
Returns:
-
-1, ifselfis smaller. -
0, if the two are equal. -
1, ifselfis larger. -
nil, if the two are incomparable.
Examples:
Example
'a' <=> 'b' # => -1 'a' <=> 'ab' # => -1 'a' <=> 'a' # => 0 'b' <=> 'a' # => 1 'ab' <=> 'a' # => 1 'a' <=> :a # => nil
Class String includes module Comparable, each of whose methods uses String#<=> for comparison.
Related: see Comparing.