diff --git a/lib/set.rb b/lib/set.rb index 08a31e29b..1e0fbbaf6 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -288,11 +288,13 @@ def each(&block) end end - def merge(other) - unless other.is_a?(Enumerable) - raise ArgumentError, "value must be enumerable" + def merge(*others) + others.each do |other| + unless other.is_a?(Enumerable) + raise ArgumentError, "value must be enumerable" + end + other.each { |element| add(element) } end - other.each { |element| add(element) } self end diff --git a/spec/library/set/merge_spec.rb b/spec/library/set/merge_spec.rb index a8e3ffc87..3ae0da827 100644 --- a/spec/library/set/merge_spec.rb +++ b/spec/library/set/merge_spec.rb @@ -16,4 +16,16 @@ -> { Set[1, 2].merge(1) }.should raise_error(ArgumentError) -> { Set[1, 2].merge(Object.new) }.should raise_error(ArgumentError) end + + ruby_version_is ""..."3.3" do + it "accepts only a single argument" do + -> { Set[].merge([], []) }.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 1)") + end + end + + ruby_version_is "3.3" do + it "accepts multiple arguments" do + Set[:a, :b].merge(Set[:b, :c], [:d]).should == Set[:a, :b, :c, :d] + end + end end