Skip to content

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

instance method

Set#compare_by_identity

() -> self
compare_by_identity → self

Sets self to compare by object identity (rather than by object content, which is the initial setting); returns self:

Example
set = Set.new
set.compare_by_identity
str = +"foo"
set.add(str)
# =>  Set["foo"]
set.include?(str)
# => true
set.add(str)
# => Set["foo"])
set.include?(+"foo")
# => false
set.add(+"foo")
# => Set["foo", "foo"])

Once set, the compare-by-identity property may not be unset.

Related: compare_by_identity?.

See also #compare_by_identity? #collect! #clear #delete also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0