Skip to content
instance method

Hash#compare_by_identity

() -> self
compare_by_identity → self

Sets self to compare keys using identity (rather than mere equality); returns self:

By default, two keys are considered to be the same key if and only if they are equal objects (per method eql?):

Example
h = {}
h['x'] = 0
h['x'] = 1 # Overwrites.
h # => {"x"=>1}

When this method has been called, two keys are considered to be the same key if and only if they are the same object:

Example
h.compare_by_identity
h['x'] = 2 # Does not overwrite.
h # => {"x"=>1, "x"=>2}

Related: compare_by_identity?; see also Methods for Comparing.

See also #compare_by_identity? #compact! #compact #default also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0