diff --git a/lib/set.rb b/lib/set.rb index ff312ee906..9140d024cf 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -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,13 +596,15 @@ 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) - if enum.instance_of?(self.class) - @hash.update(enum.instance_variable_get(:@hash)) - else - do_with_enum(enum) { |o| add(o) } + 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 diff --git a/test/test_set.rb b/test/test_set.rb index 164dc460a7..e2a0f88f5b 100644 --- a/test/test_set.rb +++ b/test/test_set.rb @@ -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