Actions
Misc #12835
closedRDoc comment of String#casecmp
Status:
Closed
Assignee:
-
Description
String#casecmp dose not return nil but raise TypError for incomparable argument.
So
* str.casecmp(other_str) -> -1, 0, +1 or nil
should be like
* str.casecmp(other_str) -> -1, 0, +1
or
* str.casecmp(other_str) -> -1, 0, +1 or raise TypeError if other_str is not comparable
Here is a example.
irb(main):001:0> "a" <=> 1
=> nil
irb(main):002:0> "a".casecmp(1)
TypeError: no implicit conversion of Fixnum into String
Japanese Reference Manual has same issue.
Updated by znz (Kazuhiro NISHIYAMA) almost 8 years ago
I created pull request for Japanese Reference Manual.
Updated by stomar (Marcus Stollsteimer) over 7 years ago
Note that String#casecmp can return nil
:
"\u{e4 f6 fc}".encode("ISO-8859-1").casecmp("\u{c4 d6 dc}") #=> nil
The relevant lines in the source code (in string.c
):
static VALUE
rb_str_casecmp(VALUE str1, VALUE str2)
{
/* ... */
enc = rb_enc_compatible(str1, str2);
if (!enc) {
return Qnil;
}
/* ... */
}
Updated by stomar (Marcus Stollsteimer) over 7 years ago
I tried to clarify this in the rdoc with r57886.
Updated by stomar (Marcus Stollsteimer) over 7 years ago
- Status changed from Open to Closed
Closing since the rdoc matches the actual behavior. Additionally I opened a new issue because raising TypeError seems to be inconsistent with other comparison methods, see #13312.
Actions
Like0
Like0Like0Like0Like0