62

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

8 Answers 8

107
[needle1, needle2].any? { |needle| haystack.include? needle }
Sign up to request clarification or add additional context in comments.

2 Comments

also works with .all? if you wanted AND logic instead of OR logic. Thanks, seph!
this is the better answer if you need to be flexible with the amount of needles. needles = [obj1, obj2, ..., objN]; needles.any? { |needle| haystack.include? needle }
68

Try parens in the expression:

 haystack.include?(needle1) || haystack.include?(needle2)

Comments

25

You can do a regex match:

haystack.match? /needle1|needle2/

Or if your needles are in an array:

haystack.match? Regexp.union(needles)

(For Ruby < 2.4, use .match without question mark.)

6 Comments

Your code has a typo, it should be match no question mark.
@SephCordovano How am I using it wrong and how can I correct it? To me it looks exactly the same as the match? method documentation example linked above.
@SephCordovano your comment is confusing. You should either expand it or delete it.
@jj_What's confusing about it? If you want to search for an array of substrings you use a union.
Please check your Ruby version before commenting. 2.4.0 has match? whereas previous Ruby versions did not have this method. Added a caveat to the answer to help address some of the comments mentioned here.
|
10
(haystack.split & [needle1, needle2]).any?

To use comma as separator: split(',')

Comments

9

For an array of substrings to search for I'd recommend

needles = ["whatever", "pretty"]

if haystack.match?(Regexp.union(needles))
  ...
end

Comments

6

To check if contains at least one of two substrings:

haystack[/whatever|pretty/]

Returns first result found

Comments

0

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.

haystack.select { |str| str.include?(needle1) || str.include?(needle2) }

and if searching partially:

haystack.select { |str| str.include?('wat') || str.include?('pre') }

Comments

0

Use or instead of ||

if haystack.include? needle1 or haystack.include? needle2

or has lower presedence than || , or is "less sticky" if you will :-)

1 Comment

Using "or" instead of "||" is generally considered as a bad idea. No matter it will actually work in that specific case. More info: github.com/rubocop/ruby-style-guide#andor- and graceful.dev/courses/the-freebies/modules/ruby-language/topic/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.