Is there a nice way to check if a string contains at least one string from an array of strings?
ruby, ruby-on-rails, ruby-on-rails-3, string
Solution
arrays_of_strings_to_check_against.map{ |o| string_1 =~ /\b#{Regexp.escape(o)}\b/ }.any?
Or even:
arrays_of_strings_to_check_against.any?{ |o| string_1 =~ /\b#{Regexp.escape(o)}\b/ }
Problem
`string.include?(other_string)` is used to check if a string contains another string. Is there a nice way to check if a string contains at least one string from an array of strings? ``` string_1 = "a monkey is an animal. dogs are fun" arrays_of_strings_to_check_against = ['banana', 'fruit', 'animal', 'dog'] ``` This would return `true`, because `string_1` contains the string `'animal'`. If we remove `'animal'` from `arrays_of_strings_to_check_against`, it would return `false`. Note that the string `'dog'` from `arrays_of_strings_to_check_against` should not match `'dogs'` from `string_1`, because it has to be a complete match. I'm using Rails 3.2.0 and Ruby 1.9.2