1

My string is

str = "my string is this one"

and my array is

arr = ["no", "nothing", "only", "is"]

So, my string includes the is in the value of my array, I want to get the result true

How can I do this?

I want to use include? met

3 Answers 3

3

To check when the whole word is included case sensitive:

(str.split & arr).any?
#⇒ true

Case insensitive:

[str.split, arr].map { |a| a.map(&:downcase) }.reduce(&:&).any?
#⇒ true

To check if it includes any of arr anyhow:

arr.any?(&str.method(:include?))
#⇒ true
Sign up to request clarification or add additional context in comments.

Comments

1
(str.downcase.split & arr.map(&:downcase)).any?
   #=> false

If it is known that all letters are of the same case.

(str.split & arr).any?
   #=> false

Comments

0

This uses a Regular expression. The good news is that it is generated for you - no syntax to learn. Other good news: it traverses the string only once.

re = Regexp.union(arr)  #your own regular expression without screws or bolts
p re.match?(str)  # => true

1 Comment

I believe you need word boundaries. For example, str = "not"; arr = ["no"] should not match. (Easy fix, nice approach.)

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.