Feature #12210
closed
Add IdentitySet class that compares members by identity
Added by tjwp (Tim Perkins) over 8 years ago.
Updated about 8 years ago.
Description
This subclass of Set handles a use case that we ran into where we needed to track instances of objects that might compare as equal.
I was surprised that there was not a core way to do this. IdentitySet allows you to do the following (trivial example using strings):
a_str = "a"
s = IdentitySet.new([a_str, a_str, "b", "b"])
p s # => #<IdentitySet: {"a", "b", "b"}>
Files
I think that rather than creating a new class, adding an option on Set.new (e.g. Set.new([a_str, a_str, "b", "b"], compare_by: :identity)) is more Ruby-like.
If you want a new class, why not start as a separate gem for it?
I submitted this as a patch because the std lib already contains one subclass of Set (SortedSet) and a commented-out implementation of another (RestrictedSet).
I don’t expect that this class will change beyond the initial implementation. Following the precedent of SortedSet, I think that including IdentitySet in the std lib improves discoverability over providing it via a gem, which seems like a more heavyweight solution for a simple, useful variation on a Set.
I was surprised that there wasn't already a standard way to do this.
- Status changed from Open to Assigned
- Assignee set to knu (Akinori MUSHA)
- Tracker changed from Misc to Feature
I'd rather add Set#compare_by_identity.
- Status changed from Assigned to Closed
Applied in changeset r56589.
Add Set#compare_by_identity and Set#compare_by_identity?
- lib/set.rb (Set#compare_by_identity, Set#compare_by_identity?):
New methods. [Feature #12210]
Note that SortedSet#compare_by_identity is unimplemented when SortedSet uses rbtree.
rbtree.readjust { |a, b| a.object_id <=> b.object_id }
comes pretty close, but rbtree treats string keys specially by storing a copy of each string key given, so it does not work as expected for string keys. There needs some way provided on the rbtree side.
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0