tags:

views:

36

answers:

1

Hi all-

I have a simple ruby question. I have an array of strings. I'd like to determine if that array contains a substring of any of the strings. As an example

a = ['cat','dog','elephant']
a.to_s.include?('ele')

Is this the best way to do it?

Thanks.

+3  A: 

a.any? should do the job.

> a = ['cat','dog','elephant']
=> ["cat", "dog", "elephant"]
> a.any? { |s| s.include?('ele') }
=> true
> a.any? { |s| s.include?('nope') }
=> false
Shadwell