Given:
arr = ["a", "b", "x"]
str = "DoDox"
EDIT: As the other comments have pointed out, this is actually the fastest way, as it breaks the evaluation when a single true statement is found:
arr.any?{|substr| str.include?(substr)}
The foldl way: I have used this example to illustrate the power of folding expresions. This solution might be somewhat elegant as an illustrative example for foldings, but it is not sufficiently performant.
arr.map{|substr| str.include?(substr)}.inject{|x,y| x || y}
The map
-function maps all possible substrings the original string str
can have. inject
iterates over the returning array and compares its constituents, using a logical or. If any substring in the array 'arr' is encountered, the result will yield true in the result of map. With the or-relation we return a true value, if any value of the result of map was true. This is slower than any?
, because every single value in the array gets evaluated even if a true
is encountered (wich would always yield a true
at the end of inject
).
["a", "b", "x"] -------> [false, false, true] ----------> true
map() inject()