From: Nobuyoshi Nakada Date: 2007-07-16T03:38:48+09:00 Subject: Re: String#rindex(other) doesn't try to convert other via to_str Hi, At Mon, 16 Jul 2007 00:27:37 +0900, Florian Gross wrote in [ruby-core:11692]: > obj = Object.new > def obj.to_str() "y" end > "ruby".index(obj) # => 3 > "ruby".rindex(obj) # ~> in `rindex': type mismatch: Object given > (TypeError) Though rindex accepts also Regexp and Fixnum, but String should be prior to them? Index: string.c =================================================================== --- string.c (revision 12802) +++ string.c (working copy) @@ -1352,4 +1352,15 @@ rb_str_rindex_m(int argc, VALUE *argv, V break; + default: { + VALUE tmp; + + tmp = rb_check_string_type(sub); + if (NIL_P(tmp)) { + rb_raise(rb_eTypeError, "type mismatch: %s given", + rb_obj_classname(sub)); + } + sub = tmp; + } + /* fall through */ case T_STRING: pos = rb_str_rindex(str, sub, pos); @@ -1374,8 +1385,4 @@ rb_str_rindex_m(int argc, VALUE *argv, V return Qnil; } - - default: - rb_raise(rb_eTypeError, "type mismatch: %s given", - rb_obj_classname(sub)); } return Qnil; -- Nobu Nakada