* lib/set.rb (Set#replace): Check if an object given is enumerable

before clearing self.  Reported by yui-knk. [GH-675]
  https://github.com/ruby/ruby/pull/675

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47085 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2014-08-06 11:28:21 +00:00
Родитель f2ff35c83d
Коммит 305be780df
3 изменённых файлов: 16 добавлений и 4 удалений

Просмотреть файл

@ -1,3 +1,9 @@
Wed Aug 6 20:25:47 2014 Akinori MUSHA <knu@iDaemons.org>
* lib/set.rb (Set#replace): Check if an object given is enumerable
before clearing self. Reported by yui-knk. [GH-675]
https://github.com/ruby/ruby/pull/675
Wed Aug 6 20:07:26 2014 Masaki Suketa <masaki.suketa@nifty.ne.jp>
* ext/win32ole/win32ole.c (olerecord_ivar_set): remove rb_str_subseq.

Просмотреть файл

@ -91,9 +91,9 @@ class Set
def do_with_enum(enum, &block) # :nodoc:
if enum.respond_to?(:each_entry)
enum.each_entry(&block)
enum.each_entry(&block) if block
elsif enum.respond_to?(:each)
enum.each(&block)
enum.each(&block) if block
else
raise ArgumentError, "value must be enumerable"
end
@ -149,12 +149,12 @@ class Set
def replace(enum)
if enum.instance_of?(self.class)
@hash.replace(enum.instance_variable_get(:@hash))
self
else
do_with_enum(enum)
clear
merge(enum)
end
self
end
# Converts the set to an array. The order of elements is uncertain.

Просмотреть файл

@ -98,6 +98,12 @@ class TC_Set < Test::Unit::TestCase
assert_same(set, ret)
assert_equal(Set['a','b','c'], set)
set = Set[1,2]
assert_raise(ArgumentError) {
set.replace(3)
}
assert_equal(Set[1,2], set)
end
def test_to_a