Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Set#merge for Ruby 3.3 #2236

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading