зеркало из https://github.com/github/ruby.git
[ruby/set] Set#merge takes many enumerable objects like Hash#merge! does
https://github.com/ruby/set/commit/becaca994d
This commit is contained in:
Родитель
aff41a3669
Коммит
454ac4cbb2
|
@ -152,7 +152,7 @@
|
|||
# If the given object is not an element in the set,
|
||||
# adds it and returns +self+; otherwise, returns +nil+.
|
||||
# - \#merge:
|
||||
# Adds each given object to the set; returns +self+.
|
||||
# Merges the elements of each given enumerable object to the set; returns +self+.
|
||||
# - \#replace:
|
||||
# Replaces the contents of the set with the contents
|
||||
# of a given enumerable.
|
||||
|
@ -596,14 +596,16 @@ class Set
|
|||
# Equivalent to Set#select!
|
||||
alias filter! select!
|
||||
|
||||
# Merges the elements of the given enumerable object to the set and
|
||||
# Merges the elements of the given enumerable objects to the set and
|
||||
# returns self.
|
||||
def merge(enum)
|
||||
def merge(*enums)
|
||||
enums.each do |enum|
|
||||
if enum.instance_of?(self.class)
|
||||
@hash.update(enum.instance_variable_get(:@hash))
|
||||
else
|
||||
do_with_enum(enum) { |o| add(o) }
|
||||
end
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
|
|
|
@ -587,10 +587,19 @@ class TC_Set < Test::Unit::TestCase
|
|||
|
||||
def test_merge
|
||||
set = Set[1,2,3]
|
||||
|
||||
ret = set.merge([2,4,6])
|
||||
assert_same(set, ret)
|
||||
assert_equal(Set[1,2,3,4,6], set)
|
||||
|
||||
set = Set[1,2,3]
|
||||
ret = set.merge()
|
||||
assert_same(set, ret)
|
||||
assert_equal(Set[1,2,3], set)
|
||||
|
||||
set = Set[1,2,3]
|
||||
ret = set.merge([2,4,6], Set[4,5,6])
|
||||
assert_same(set, ret)
|
||||
assert_equal(Set[1,2,3,4,5,6], set)
|
||||
end
|
||||
|
||||
def test_subtract
|
||||
|
|
Загрузка…
Ссылка в новой задаче