I have an array:
a = [1,1,2,3,4]
And more arrays:
b =[[1,2,3], [1,1,4], [7,3,4], [1,5,6,1]]
For each element in b, b_i, I want to know:
- is there some
b_isuch thata & b_i == b_i, and - what is that
b_i
This is what I am thinking
def get_matching(a, b)
b.each {|b_i|
return b_i if (a & b_i) == b_i
}
end
Where can I check whether the return value is nil or not to determine the answer to the first question? Though, maybe I can implement them as two separate functions so that checking whether such a matching exists doesn’t need to actually return the matching.
Assume I only need the first matching if there are many.
Is there a more efficient way to do this?
This is probably not any more efficient but it is a little more ruby-esque using Enumerable#detect