3

I have an array of strings:

string_array = ['memberid', 'membershiptype', 'date']

Now I need to check whether or not this array contains 'id', and I want it to return true.

I know I can do string_array.include? 'memberid', but I need to find a substring within that string of 'id'.

1 Answer 1

6

string_array.any? { |e| e.include? 'id' }

any? returns true if any of the elements in the array are true for the given predicate (i.e. the block passed to any?)

2
  • No problem. If it answered your question, please mark it as the answer. Commented Oct 7, 2013 at 19:03
  • string_array.any?{|x| x['id'] } and !string_array.grep(/id/).empty? also works.
    – hirolau
    Commented Oct 17, 2013 at 14:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.