aboutsummaryrefslogtreecommitdiffstats
path: root/lib/set.rb
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-05 09:23:14 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-05 09:23:14 +0000
commit5710defbe621766796d973b9ef6451a94d6d7eff (patch)
tree7a5669bc570cc54e8fed7c32b1e11b268af2f043 /lib/set.rb
parent1e098905e13cf1e6e589ea9f9819c147b8632505 (diff)
downloadruby-5710defbe621766796d973b9ef6451a94d6d7eff.tar.gz
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56589 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/set.rb')
-rw-r--r--lib/set.rb22
1 files changed, 20 insertions, 2 deletions
diff --git a/lib/set.rb b/lib/set.rb
index e77af5de2e..43c388ca90 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -3,7 +3,7 @@
#
# set.rb - defines the Set class
#++
-# Copyright (c) 2002-2013 Akinori MUSHA <knu@iDaemons.org>
+# Copyright (c) 2002-2016 Akinori MUSHA <knu@iDaemons.org>
#
# Documentation by Akinori MUSHA and Gavin Sinclair.
#
@@ -37,7 +37,8 @@
# Set uses Hash as storage, so you must note the following points:
#
# * Equality of elements is determined according to Object#eql? and
-# Object#hash.
+# Object#hash. Use Set#compare_by_identity to make a set compare
+# its elements by their identity.
# * Set assumes that the identity of each element does not change
# while it is stored. Modifying an element of a set will render the
# set to an unreliable state.
@@ -91,6 +92,23 @@ class Set
end
end
+ # Makes the set compare its elements by their identity and returns
+ # self. This method may not be supported by all subclasses of Set.
+ def compare_by_identity
+ if @hash.respond_to?(:compare_by_identity)
+ @hash.compare_by_identity
+ self
+ else
+ raise NotImplementedError, "#{self.class.name}\##{__method__} is not implemented"
+ end
+ end
+
+ # Returns true if the set will compare its elements by their
+ # identity. Also see Set#compare_by_identity.
+ def compare_by_identity?
+ @hash.respond_to?(:compare_by_identity?) && @hash.compare_by_identity?
+ end
+
def do_with_enum(enum, &block) # :nodoc:
if enum.respond_to?(:each_entry)
enum.each_entry(&block) if block