Using Intersection Set ( Array #: & ):
(myarray & ["val1", "val2", "val3", "val4"]).present?
You can also execute a loop ( any? Will stop on the first entry):
myarray.any? { |x| ["val1", "val2", "val3", "val4"].include?(x) }
This is normal for small arrays, in general it is better to have O (1) predicates:
values = ["val1", "val2", "val3", "val4"].to_set myarray.any? { |x| values.include?(x) }
With Ruby> = 2.1 use Set # intersect :
myarray.to_set.intersect?(values.to_set)
tokland Nov 06 '11 at 10:10 2011-11-06 10:10
source share