Feature #15198
Array#intersect?
Description
I frequently find myself needing to determine if two arrays intersect but not actually caring about the intersection, so I write code like:
(a1 & a2).any?
It would be nice to have an intersect? convenience method on Array to perform this query.
Related issues
Updated by shyouhei (Shyouhei Urabe) over 2 years ago
Interesting. Can you share a bit more detail about your "if two arrays intersect but not actually caring about the intersection" use case? For instance if you have open sourced such code, a URL for it helps us a lot.
If you have zero interest to the intersection itself I think you can avoid creating the temporary array.
Updated by c4am95 (Travis Hunter) over 2 years ago
The most recent example I encountered was authorizing a user in a Rails endpoint. Each user has a list of abilities, and each endpoint has a list of abilities that is authorized to perform the action. We just need to check if there is an intersection between the two lists.
I updated the PR to avoid creating the intermediate array.
Updated by shevegen (Robert A. Heiler) over 2 years ago
Demonstrated use cases helps the core team & matz assess on the usefulness of a proposed
change, which can help in accepting issue requests ultimately in the long run (if the
use case is considered sufficiently useful). :)
Updated by c4am95 (Travis Hunter) over 2 years ago
I threw together an example in a gist which is very similar to the use case I described. I have also run into numerous other situations where this functionality would have been useful.
I added some comments in the gist but I'll cross-post them here for reference:
The current behavior creates an intermediate array for both intersection tests when the resulting array is clearly not needed. With the desired behavior, we could avoid creating the intermediate array and produce a faster best case runtime.
Updated by c4am95 (Travis Hunter) over 2 years ago
It also seems like it comes up fairly commonly on stackoverflow/blogs:
https://stackoverflow.com/questions/2603895/how-can-i-check-if-a-ruby-array-includes-one-of-several-values
https://www.ruby-forum.com/t/find-if-an-array-has-any-element-present-in-another-array/166877
https://irb.rocks/comparison-arrays-ruby/
https://tosbourn.com/set-intersection-in-ruby/
Updated by matz (Yukihiro Matsumoto) over 1 year ago
- Has duplicate Feature #15976: Add Array#overlap? for whether the intersection of 2 arrays is non empty? added
Updated by Dan0042 (Daniel DeLorme) over 1 year ago
It might make sense to use ary1.to_set.intersect?(ary2)
. That way it makes explicit the fact that ary1 must be converted to a set. But Set#intersect?
would have to support any Enumerable.
Updated by c4am95 (Travis Hunter) over 1 year ago
I tried setting that up here: https://github.com/ruby/ruby/pull/2598
I ignored infinite ranges for now
Updated by marcandre (Marc-Andre Lafortune) 8 months ago
- Related to Feature #16928: Array#include_all? & Array#include_any? added