I've seen a couple threads here that shows the most efficient way to check a string against an array of substrings and return a boolean if there is a match.
str = "foobazbar"
arr = ["baz", "clowns"]
result = arr.any? { |substring| str.include?(substring) } # solution
result => true
However, as elegant and efficient as that solution is, is there any way to return the match itself? Using the example from above, I also want to know that the match was baz. What is the best way to accomplish this?
"foobazbar"does not contain the substring"baz, clowns", soresultshould befalse."baz"and"clowns"are the substrings you are looking for, you will first have to parse the contents of the array.arrline. Fixed it. Seems like the solution posted by @Ursus works