* lib/set.rb: Add checks that passed argument is Enumerable. [ruby-core:23844]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26120 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2009-12-19 00:29:54 +00:00
parent 25bccc44d4
commit a88cc058f0
2 changed files with 14 additions and 2 deletions

View File

@ -1,3 +1,8 @@
Sat Dec 19 09:29:22 2009 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/set.rb: Add checks that passed argument is Enumerable.
[ruby-core:23844]
Wed Dec 16 20:28:46 2009 Kazuhiro NISHIYAMA <zn@mbf.nifty.com> Wed Dec 16 20:28:46 2009 Kazuhiro NISHIYAMA <zn@mbf.nifty.com>
* test/ruby/envutil.rb: fix a typo in assert message. * test/ruby/envutil.rb: fix a typo in assert message.

View File

@ -70,6 +70,7 @@ class Set
enum.nil? and return enum.nil? and return
if block if block
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
enum.each { |o| add(block[o]) } enum.each { |o| add(block[o]) }
else else
merge(enum) merge(enum)
@ -122,6 +123,7 @@ class Set
if enum.class == self.class if enum.class == self.class
@hash.replace(enum.instance_eval { @hash }) @hash.replace(enum.instance_eval { @hash })
else else
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
clear clear
enum.each { |o| add(o) } enum.each { |o| add(o) }
end end
@ -279,6 +281,7 @@ class Set
if enum.instance_of?(self.class) if enum.instance_of?(self.class)
@hash.update(enum.instance_variable_get(:@hash)) @hash.update(enum.instance_variable_get(:@hash))
else else
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
enum.each { |o| add(o) } enum.each { |o| add(o) }
end end
@ -288,6 +291,7 @@ class Set
# Deletes every element that appears in the given enumerable object # Deletes every element that appears in the given enumerable object
# and returns self. # and returns self.
def subtract(enum) def subtract(enum)
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
enum.each { |o| delete(o) } enum.each { |o| delete(o) }
self self
end end
@ -310,6 +314,7 @@ class Set
# Returns a new set containing elements common to the set and the # Returns a new set containing elements common to the set and the
# given enumerable object. # given enumerable object.
def &(enum) def &(enum)
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
n = self.class.new n = self.class.new
enum.each { |o| n.add(o) if include?(o) } enum.each { |o| n.add(o) if include?(o) }
n n
@ -637,6 +642,7 @@ end
# end # end
# #
# def replace(enum) # def replace(enum)
# enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
# clear # clear
# enum.each { |o| add(o) } # enum.each { |o| add(o) }
# #
@ -644,6 +650,7 @@ end
# end # end
# #
# def merge(enum) # def merge(enum)
# enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
# enum.each { |o| add(o) } # enum.each { |o| add(o) }
# #
# self # self
@ -711,10 +718,10 @@ class TC_Set < Test::Unit::TestCase
Set.new([1,2]) Set.new([1,2])
Set.new('a'..'c') Set.new('a'..'c')
} }
assert_raises(NoMethodError) { assert_raises(ArgumentError) {
Set.new(false) Set.new(false)
} }
assert_raises(NoMethodError) { assert_raises(ArgumentError) {
Set.new(1) Set.new(1)
} }
assert_raises(ArgumentError) { assert_raises(ArgumentError) {