summaryrefslogtreecommitdiff
path: root/lib/set.rb
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-19 02:22:11 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-19 02:22:11 +0000
commitad78cf4ea8613c7e1790c5e3a2718a35fe32115f (patch)
tree3d1c8d8aad3a4600c911afb6b05484ba1d556aa8 /lib/set.rb
parent74201103a9bda8488672c8e7e5f413d4611e356b (diff)
Define Set#to_set so that aSet.to_set returns self.
* lib/set.rb (Set#to_set): Define Set#to_set so that aSet.to_set returns self. [Fixes GH-359] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42051 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/set.rb')
-rw-r--r--lib/set.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/set.rb b/lib/set.rb
index a9aa7e7..47fcc3e 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -148,6 +148,16 @@ class Set
@hash.keys
end
+ # Returns self if no arguments are given. Otherwise, converts the
+ # set to another with klass.new(self, *args, &block).
+ #
+ # In subclasses, returns klass.new(self, *args, &block) unless
+ # overridden.
+ def to_set(klass = Set, *args, &block)
+ return self if instance_of?(Set) && klass == Set && block.nil? && args.empty?
+ klass.new(self, *args, &block)
+ end
+
def flatten_merge(set, seen = Set.new) # :nodoc:
set.each { |e|
if e.is_a?(Set)