The easiest thing would be to do a set intersection and see what you get from that:
intersection = array1 & array2
if intersection.length == array1.length
# Everything in array1 is in array2
end
That would, of course, fail if array1
had duplicates as the intersection will automatically compress those. But we have uniq
to take care of that:
intersection = array1 & array2
if intersection.length == array1.uniq.length
# Everything in array1 is in array2
end
If you're expecting duplicates in your arrays then you'd be better off working with instances of Set rather than arrays:
require 'set'
s1 = Set.new(array1)
s2 = Set.new(array2)
if((s1 & s2) == s1)
# Everything in array1 is in array2
end
Or use subset?
to better match your intentions:
if(s1.subset?(s2))
# Everything in array1 is in array2
end
Using sets will take care of your duplicate issues with less noise than having to use uniq
all the time. There would, of course, be a bit of extra overhead but you should optimize for clarity before performance (make it work then make it fast only if it is too slow).