Check whether a string contains one of multiple su

2019-01-31 17:31发布

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

7条回答
我命由我不由天
2楼-- · 2019-01-31 18:27
[needle1, needle2].any? { |needle| haystack.include? needle }
查看更多
登录 后发表回答