diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-08-06 11:28:21 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-08-06 11:28:21 +0000 |
commit | 305be780df86d3bd5f1b93848101b477083452ed (patch) | |
tree | 55d8ff069c370778bc67b2fd80c48a327d50ef30 | |
parent | f2ff35c83dde349b5b143ca04afb09955760bcaa (diff) | |
download | ruby-305be780df86d3bd5f1b93848101b477083452ed.tar.gz |
* 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
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | lib/set.rb | 8 | ||||
-rw-r--r-- | test/test_set.rb | 6 |
3 files changed, 16 insertions, 4 deletions
@@ -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. diff --git a/lib/set.rb b/lib/set.rb index db57594d0a..f00cfac0c5 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -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. diff --git a/test/test_set.rb b/test/test_set.rb index 169136f0d9..961578b696 100644 --- a/test/test_set.rb +++ b/test/test_set.rb @@ -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 |