简体   繁体   中英

How to check whether an array contains a substring within a string in Ruby?

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' string_array.include? 'memberid' , but I need to find a substring within that string of 'id' .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM