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?)

Sign up to request clarification or add additional context in comments.

2 Comments

No problem. If it answered your question, please mark it as the answer.
string_array.any?{|x| x['id'] } and !string_array.grep(/id/).empty? also works.

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.