String#==

C

Charles Mills

String#== returns nil when the object being compared does not have a
"to_str" method.
For example:
irb> "hey" == 1
=> nil
Is that correct?
Here is the code (from 1.8.1):
/* taken from string.c line 792 */
static VALUE
rb_str_equal(str1, str2)
VALUE str1, str2;
{
if (str1 == str2) return Qtrue;
if (TYPE(str2) != T_STRING) {
if (!rb_respond_to(str2, rb_intern("to_str"))) {
return Qnil;
}
return rb_equal(str2, str1);
}
if (RSTRING(str1)->len == RSTRING(str2)->len &&
rb_str_cmp(str1, str2) == 0) {
return Qtrue;
}
return Qfalse;
}
/* ... snip ... */
rb_define_method(rb_cString, "==", rb_str_equal, 1);
/* end */

-Charlie
 
M

Michael Campbell

String#== returns nil when the object being compared does not have a
"to_str" method.
For example:
irb> "hey" == 1
=> nil
Is that correct?


I'm in no way authoritative, but it makes sense to me. If you're
trying to see if a String is "equal" to something that cant be
converted to a string, 'nil' (as false) seems logical.
 
Y

Yukihiro Matsumoto

Hi,

In message "String#=="

|String#== returns nil when the object being compared does not have a
|"to_str" method.

In 1.8.2 and 1.9, it will return false.

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top