Anything like Array.contains? method

P

Paul Sholtz

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..
 
J

Jos Backus

[Note: parts of this message were removed to make it a legal post.]

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..
Array.include?
 


$B209qMZ(B

2011/3/3 Paul Sholtz said:
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?
 
K

Karuppasamy M

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

a =3D [ "a", "b", "c" ]
a.index("b") #=3D> 1
a.index("z") #=3D> nil

M. Karuppasamy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top