## Feature #6588

### Set#intersect?

**Description**

There is `Set#superset?`

, `Set#subset?`

with their `proper`

variants, but there is no `Set#intersect?`

nor `Set#disjoint?`

To check if two sets intersect, one can do

set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can instead do the iteration manually:

other.any? { |x| set.include?(x) }

I think it would be natural to add `Set#intersect?`

and its reverse `Set#disjoint?`

class Set

def intersect?(enum)

enum.any? { |x| include?(x) }

end

end

Maybe it would be worth it to optimize it if enum is a larger Set by starting it with

return any? { |x| enum.include?(x) } if enum.is_a?(Set) && enum.size > size

### Associated revisions

Add Set#intersect? and #disjoint?.

- lib/set.rb (Set#intersect?, Set#disjoint?): Add new methods for testing if two sets have any element in common. [Feature #6588] Based on the code by marcandre.

Add Set#intersect? and #disjoint?.

- lib/set.rb (Set#intersect?, Set#disjoint?): Add new methods for testing if two sets have any element in common. [Feature #6588] Based on the code by marcandre.

Add Set#intersect? and #disjoint?.

- lib/set.rb (Set#intersect?, Set#disjoint?): Add new methods for testing if two sets have any element in common. [Feature #6588] Based on the code by marcandre.

### History

#### #1 [ruby-core:46459] Updated by Yusuke Endoh about 4 years ago

**Status**changed from*Open*to*Assigned*

#### #2 [ruby-core:48259] Updated by Yutaka HARA almost 4 years ago

**Target version**changed from*2.0.0*to*next minor*

#### #3 [ruby-core:49242] Updated by Marc-Andre Lafortune over 3 years ago

Comment about these simple features would be appreciated.

#### #4 [ruby-core:49252] Updated by Alexey Muranov over 3 years ago

+1. Maybe `#meet?`

instead of `#intersect?`

? It can be argued that any set intersects any other, just the intersection is sometimes empty :).

#### #5 [ruby-core:49258] Updated by Marc-Andre Lafortune over 3 years ago

alexeymuranov (Alexey Muranov) wrote:

+1.

Thanks for the +1

It can be argued that any set intersects any other, just the intersection is sometimes empty :).

No, I believe it would be wrong to argue that. From wikipedia: "We say that A intersects B if A intersects B at some element"

Moreover: "We say that A and B are disjoint if A does not intersect B. In plain language, they have no elements in common"

I believe that both `intersect?`

and `disjoint?`

are the established terms for the concept I'm proposing.

#### #6 [ruby-core:56217] Updated by Akinori MUSHA about 3 years ago

OK, accepted. I'll work on it.

#### #7 Updated by Akinori MUSHA almost 3 years ago

**Status**changed from*Assigned*to*Closed***% Done**changed from*0*to*100*

This issue was solved with changeset r42253.

Marc-Andre, thank you for reporting this issue.

Your contribution to Ruby is greatly appreciated.

May Ruby be with you.

Add Set#intersect? and #disjoint?.

#### #8 [ruby-core:56268] Updated by Akinori MUSHA almost 3 years ago

I followed superset?() and the like, and made the new methods accept only a set for the moment, because I couldn't come up with an idea of how to deal with Range. For example:

- if Set[2].intersect?(1.5..2.5) should return true
- if Set[2].intersect?(3..(1.0/0)) should immediately return false using some knowledge on Range