Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Hash#==

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

Returns whether all of the following are true:

  • other is a Hash object (or can be converted to one).

  • self and other have the same keys (regardless of order).

  • For each key key, self[key] == other[key].

Examples:

Example
h =  {foo: 0, bar: 1}
h == {foo: 0, bar: 1} # => true   # Equal entries (same order)
h == {bar: 1, foo: 0} # => true   # Equal entries (different order).
h == 1                            # => false  # Object not a hash.
h == {}                           # => false  # Different number of entries.
h == {foo: 0, bat: 1} # => false  # Different key.
h == {foo: 0, bar: 2} # => false  # Different value.

Related: see Methods for Comparing.

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