1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/set] Allow Set#intersect? and #disjoint? to accept array argument

Implements [Feature #17838]

https://github.com/ruby/set/commit/d9b389bafa
This commit is contained in:
Jeremy Evans 2021-06-15 11:08:45 -07:00 committed by Hiroshi SHIBATA
parent 27fb9d272d
commit 571dafdc7f
2 changed files with 13 additions and 2 deletions

View file

@ -474,7 +474,14 @@ class 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

View file

@ -354,13 +354,17 @@ class TC_Set < Test::Unit::TestCase
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) {
@ -378,7 +382,7 @@ class TC_Set < Test::Unit::TestCase
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])