Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
String#==
(untyped other) -> bool
self == other → true or false
string.c
View on GitHub ↗
VALUE
rb_str_equal(VALUE str1, VALUE str2)
{
if (str1 == str2) return Qtrue;
if (!RB_TYPE_P(str2, T_STRING)) {
if (!rb_respond_to(str2, idTo_str)) {
return Qfalse;
}
return rb_equal(str2, str1);
}
return rb_str_eql_internal(str1, str2);
}
Returns whether other is equal to self.
When other is a string, returns whether other has the same length and content as self:
Example
s = 'foo' s == 'foo' # => true s == 'food' # => false s == 'FOO' # => false
Returns false if the two strings’ encodings are not compatible:
Example
"\u{e4 f6 fc}".encode(Encoding::ISO_8859_1) == ("\u{c4 d6 dc}") # => false
When other is not a string:
-
If
otherresponds to methodto_str,other == selfis called and its return value is returned. -
If
otherdoes not respond toto_str,falseis returned.
Related: Comparing.