[ruby-core:74534] [Ruby trunk Misc#12210] Add IdentitySet class that compares members by identity

From: tjwp@...
Date: 2016-03-24 12:02:05 UTC
List: ruby-core #74534
Issue #12210 has been updated by Tim Perkins.


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.

----------------------------------------
Misc #12210: Add IdentitySet class that compares members by identity
https://bugs.ruby-lang.org/issues/12210#change-57657

* Author: Tim Perkins
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
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):

~~~ruby
  a_str = "a"
  s = IdentitySet.new([a_str, a_str, "b", "b"])

  p s # => #<IdentitySet: {"a", "b", "b"}>
~~~

---Files--------------------------------
identity_set.diff (1.18 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next