Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

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

like image 788
Luigi Avatar asked Nov 23 '25 05:11

Luigi


1 Answers

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

like image 178
Eric Andres Avatar answered Nov 25 '25 19:11

Eric Andres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!