Anything like Array.contains? method

Is there a method in the Ruby array object that gives anything like a
“contains?” method, to test if an object is already in the Array?

For the time being, I’ve extended the Array class as follows:

http://pastie.org/1627594

I wondered if this was a good way, or if there was already something in
the API that allowed for this. I couldn’t seem to find anything…

On Wed, Mar 2, 2011 at 10:12 PM, Paul S. [email protected]
wrote:

Array.include?

2011/3/3 Paul S. [email protected]:

Is there a method in the Ruby array object that gives anything like a
“contains?” method, to test if an object is already in the Array?

How about include?

Returns the index of the first object in self such that is == to
obj.
Returns nil if no match is found.

a = [ “a”, “b”, “c” ]
a.index(“b”) #=> 1
a.index(“z”) #=> nil

M. Karuppasamy

“屋国遥” [email protected] wrote in post #985112:

2011/3/3 Paul S. [email protected]:

Is there a method in the Ruby array object that gives anything like a
“contains?” method, to test if an object is already in the Array?

How about include?

Yes, that’s want I wanted… :slight_smile:

thanks…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs