Skip to content

Commit

Permalink
Merge pull request #2236 from herwinw/set_merge
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m committed Sep 25, 2024
2 parents 556eff8 + 4a3c4ac commit 0ee5a76
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions spec/library/set/merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0ee5a76

Please sign in to comment.