Skip to content

Commit

Permalink
Allow Set#intersect? and #disjoint? to accept array argument
Browse files Browse the repository at this point in the history
Implements [Feature #17838]
  • Loading branch information
jeremyevans committed Jun 15, 2021
1 parent a9a82de commit d9b389b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,14 @@ def <=>(set)
# Set[1, 2, 3].intersect? Set[4, 5] #=> false
# Set[1, 2, 3].intersect? Set[3, 4] #=> true
def intersect?(set)
set.is_a?(Set) or raise ArgumentError, "value must be a set"
case set
when Set
# nothing
when Array
Set.new(set)
else
raise ArgumentError, "value must be a set or array"
end
if size < set.size
any? { |o| set.include?(o) }
else
Expand Down
6 changes: 5 additions & 1 deletion test/test_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,17 @@ def assert_intersect(expected, set, other)
case expected
when true
assert_send([set, :intersect?, other])
assert_send([set, :intersect?, other.to_a])
assert_send([other, :intersect?, set])
assert_not_send([set, :disjoint?, other])
assert_not_send([set, :disjoint?, other.to_a])
assert_not_send([other, :disjoint?, set])
when false
assert_not_send([set, :intersect?, other])
assert_not_send([set, :intersect?, other.to_a])
assert_not_send([other, :intersect?, set])
assert_send([set, :disjoint?, other])
assert_send([set, :disjoint?, other.to_a])
assert_send([other, :disjoint?, set])
when Class
assert_raise(expected) {
Expand All @@ -378,7 +382,7 @@ def test_intersect?
set = Set[3,4,5]

assert_intersect(ArgumentError, set, 3)
assert_intersect(ArgumentError, set, [2,4,6])
assert_intersect(true, set, Set[2,4,6])

assert_intersect(true, set, set)
assert_intersect(true, set, Set[2,4])
Expand Down

0 comments on commit d9b389b

Please sign in to comment.