Skip to content

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

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 other responds to method to_str, other == self is called and its return value is returned.

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

Related: Comparing.

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