When dealing with Arrays in Ruby we frequently come across cases, where we want to find the intersection of arrays.
Our previous blog
which was used to return the common elements between arrays as shown below:
But in few cases, we only want to check if arrays intersect without knowing the result.
Before Ruby 3.1, we would chain the
intersection method with
to check if the result is true or false.
The above approach will first compute the intersection array and then evaluate
#empty? on the result.
To save memory and improve performance, Ruby 3.1
Array#intersect? method which returns
true if two arrays
have at least one element in common, otherwise returns
#intersection method which accepts multiple arrays as argument we cannot pass multiple arrays to