Skip to content
instance method

String#==

(untyped other) -> bool
self == object → true or false

Returns whether object is equal to self.

When object is a string, returns whether object 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 object is not a string:

  • If object responds to method to_str, object == self is called and its return value is returned.

  • If object does not respond to to_str, false is returned.

Related: Comparing.

See also #<=> #=== #<< #=~ also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0