I've got a long string-variable and want to find out whether it contains one of two substrings.
e.g.
haystack = 'this one is pretty long'
needle1 = 'whatever'
needle2 = 'pretty'
Now I'd need a disjunction like this which doesn't work in Ruby though:
if haystack.include? needle1 || haystack.include? needle2
puts "needle found within haystack"
end
I was trying to find simple way to search multiple substrings in an array and end up with below which answers the question as well. I've added the answer as I know many geeks consider other answers and not the accepted one only.
and if searching partially:
Try parens in the expression:
For an array of substrings to search for I'd recommend
To check if contains at least one of two substrings:
Returns first result found
To use comma as separator:
split(',')
If Ruby 2.4, you can do a regex match using
|
(or):Or if your strings are in an array:
(For Ruby < 2.4, use
.match
without question mark.)