Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Compares self and other_string, returning:
  • -1 if other_string is larger.
  • 0 if the two are equal.
  • 1 if other_string is smaller.
  • 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
 
               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);
}
            

Was this page useful?