Topic: is it possible to search for a string in an array without iterating?

Say A = ["hello", "ruby", "world"]

Is it possible to find out if the string "rub" exists in A without iterating through each element?  For e.g. something similar to A.include?("ruby")

Re: is it possible to search for a string in an array without iterating?

To access an array, which inherently has elements, 'without iterating through each element', is on the face of it, impossible.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: is it possible to search for a string in an array without iterating?

In that case I'm assuming A.include?("ruby") must also be iterating through all it's elements - is there a built in function that lets you search through the array then?

Re: is it possible to search for a string in an array without iterating?

It does, as do things like detect, reject, etc.

Take a look at Enumerable, the Array class implements these methods:
http://apidock.com/ruby/Enumerable

Rob Anderton
TheWebFellas

Re: is it possible to search for a string in an array without iterating?

You can do the following

Array.select{|v| v =~ /element/}

This will return another array with those lines that are matching.  I used this recently to search the contents of a file that I read in using readlines.