summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorocean <ocean@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-06-25 06:22:05 +0000
committerocean <ocean@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-06-25 06:22:05 +0000
commit66e5af96b2e52e31943e9ecca49809ea342a9d8e (patch)
tree09dda248972729dfe24354fb7f90a1a70f535c18 /lib
parent52a590f0ccd8655ed719e0e01c869508fa3f04a3 (diff)
* lib/set.rb (Set#==): [ruby-dev:25206] (ported from ruby_1_8 branch)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8663 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/set.rb24
1 files changed, 14 insertions, 10 deletions
diff --git a/lib/set.rb b/lib/set.rb
index 5fc61c12f7..586a5d9f4d 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -193,7 +193,7 @@ class Set
# Adds the given object to the set and returns self. Use +merge+ to
# add several elements at once.
def add(o)
- @hash[o] = o
+ @hash[o] = true
self
end
alias << add
@@ -251,7 +251,7 @@ class Set
# Merges the elements of the given enumerable object to the set and
# returns self.
def merge(enum)
- if enum.class == self.class
+ if enum.is_a?(Set)
@hash.update(enum.instance_eval { @hash })
else
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
@@ -291,7 +291,7 @@ class Set
def &(enum)
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
n = self.class.new
- enum.each { |o| include?(o) and n.add(o) }
+ enum.each { |o| n.add(o) if include?(o) }
n
end
alias intersection & ##
@@ -313,7 +313,8 @@ class Set
set.is_a?(Set) && size == set.size or return false
- set.all? { |o| @hash.value?(o) }
+ hash = @hash.dup
+ set.all? { |o| hash.include?(o) }
end
def hash # :nodoc:
@@ -321,7 +322,8 @@ class Set
end
def eql?(o) # :nodoc:
- @hash.hash == o.hash
+ return false unless o.is_a?(Set)
+ @hash.eql?(o.instance_eval{@hash})
end
# Classifies the set by the return value of the given block and
@@ -471,7 +473,7 @@ class SortedSet < Set
def add(o)
@keys = nil
- @hash[o] = o
+ @hash[o] = true
self
end
alias << add
@@ -556,7 +558,7 @@ end
# if @proc.arity == 2
# instance_eval %{
# def add(o)
-# @hash[o] = o if @proc.call(self, o)
+# @hash[o] = true if @proc.call(self, o)
# self
# end
# alias << add
@@ -565,7 +567,7 @@ end
# if include?(o) || !@proc.call(self, o)
# nil
# else
-# @hash[o] = o
+# @hash[o] = true
# self
# end
# end
@@ -588,7 +590,9 @@ end
# else
# instance_eval %{
# def add(o)
-# @hash[o] = o if @proc.call(o)
+# if @proc.call(o)
+# @hash[o] = true
+# end
# self
# end
# alias << add
@@ -597,7 +601,7 @@ end
# if include?(o) || !@proc.call(o)
# nil
# else
-# @hash[o] = o
+# @hash[o] = true
# self
# end
# end