summaryrefslogtreecommitdiff
path: root/lib/set.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-02-02 08:54:52 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-02-02 08:54:52 +0000
commit970e90dd1572994d3d9229725e12d47b3674fcbf (patch)
tree12541348cc1fc2b1ec2e3e87aa2b526c5d34a9c7 /lib/set.rb
parentbafb881c1f22781aac347cdabb845798a7753180 (diff)
* enum.c (enum_each_entry): new method #each_entry to pack values
from yield into an array. * lib/set.rb (Set#merge): use Enumerable#each_entry to implement Set compatible to 1.8 behavior. [ruby-core:27985] * lib/set.rb: replace is_a?(Enumerable) with respond_to?(:each) for duck typing. * lib/set.rb (SortedSet#add): typo fixed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26540 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/set.rb')
-rw-r--r--lib/set.rb30
1 files changed, 15 insertions, 15 deletions
diff --git a/lib/set.rb b/lib/set.rb
index e28ab0f..3e22a8c 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -70,8 +70,8 @@ class Set
enum.nil? and return
if block
- enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
- enum.each { |o| add(block[o]) }
+ enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
+ enum.each_entry { |o| add(block[o]) }
else
merge(enum)
end
@@ -123,9 +123,9 @@ class Set
if enum.class == self.class
@hash.replace(enum.instance_eval { @hash })
else
- enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
+ enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
clear
- enum.each { |o| add(o) }
+ enum.each_entry { |o| add(o) }
end
self
@@ -281,8 +281,8 @@ class Set
if enum.instance_of?(self.class)
@hash.update(enum.instance_variable_get(:@hash))
else
- enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
- enum.each { |o| add(o) }
+ enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
+ enum.each_entry { |o| add(o) }
end
self
@@ -291,8 +291,8 @@ class Set
# Deletes every element that appears in the given enumerable object
# and returns self.
def subtract(enum)
- enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
- enum.each { |o| delete(o) }
+ enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
+ enum.each_entry { |o| delete(o) }
self
end
@@ -314,9 +314,9 @@ class Set
# Returns a new set containing elements common to the set and the
# given enumerable object.
def &(enum)
- enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
+ enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
n = self.class.new
- enum.each { |o| n.add(o) if include?(o) }
+ enum.each_entry { |o| n.add(o) if include?(o) }
n
end
alias intersection & ##
@@ -514,7 +514,7 @@ class SortedSet < Set
end
def add(o)
- o.respond_to?(:<=>) or raise ArgumentError, "value must repond to <=>"
+ o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
super
end
alias << add
@@ -642,16 +642,16 @@ end
# end
#
# def replace(enum)
-# enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
+# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
# clear
-# enum.each { |o| add(o) }
+# enum.each_entry { |o| add(o) }
#
# self
# end
#
# def merge(enum)
-# enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
-# enum.each { |o| add(o) }
+# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
+# enum.each_entry { |o| add(o) }
#
# self
# end