mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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:
parent
f2ff35c83d
commit
305be780df
3 changed files with 16 additions and 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>
|
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.
|
* ext/win32ole/win32ole.c (olerecord_ivar_set): remove rb_str_subseq.
|
||||||
|
|
|
@ -91,9 +91,9 @@ class Set
|
||||||
|
|
||||||
def do_with_enum(enum, &block) # :nodoc:
|
def do_with_enum(enum, &block) # :nodoc:
|
||||||
if enum.respond_to?(:each_entry)
|
if enum.respond_to?(:each_entry)
|
||||||
enum.each_entry(&block)
|
enum.each_entry(&block) if block
|
||||||
elsif enum.respond_to?(:each)
|
elsif enum.respond_to?(:each)
|
||||||
enum.each(&block)
|
enum.each(&block) if block
|
||||||
else
|
else
|
||||||
raise ArgumentError, "value must be enumerable"
|
raise ArgumentError, "value must be enumerable"
|
||||||
end
|
end
|
||||||
|
@ -149,12 +149,12 @@ class Set
|
||||||
def replace(enum)
|
def replace(enum)
|
||||||
if enum.instance_of?(self.class)
|
if enum.instance_of?(self.class)
|
||||||
@hash.replace(enum.instance_variable_get(:@hash))
|
@hash.replace(enum.instance_variable_get(:@hash))
|
||||||
|
self
|
||||||
else
|
else
|
||||||
|
do_with_enum(enum)
|
||||||
clear
|
clear
|
||||||
merge(enum)
|
merge(enum)
|
||||||
end
|
end
|
||||||
|
|
||||||
self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Converts the set to an array. The order of elements is uncertain.
|
# 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_same(set, ret)
|
||||||
assert_equal(Set['a','b','c'], set)
|
assert_equal(Set['a','b','c'], set)
|
||||||
|
|
||||||
|
set = Set[1,2]
|
||||||
|
assert_raise(ArgumentError) {
|
||||||
|
set.replace(3)
|
||||||
|
}
|
||||||
|
assert_equal(Set[1,2], set)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_to_a
|
def test_to_a
|
||||||
|
|
Loading…
Add table
Reference in a new issue