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

···

--
Posted via http://www.ruby-forum.com/.

Array.include?

···

On Wed, Mar 2, 2011 at 10:12 PM, Paul Sholtz <paul.sholtz@gmail.com> wrote:

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

--
Jos Backus
jos at catnook.com

How about include?

···

2011/3/3 Paul Sholtz <paul.sholtz@gmail.com>:

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?

--
Haruka YAGNI
hyagni@gmail.com

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

···

On Thu, Mar 3, 2011 at 11:49 AM, 屋国遥 <hyagni@gmail.com> wrote:

2011/3/3 Paul Sholtz <paul.sholtz@gmail.com>:
> 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?
--
Haruka YAGNI
hyagni@gmail.com

"屋国遥" <hyagni@gmail.com> wrote in post #985112:

···

2011/3/3 Paul Sholtz <paul.sholtz@gmail.com>:

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

--
Posted via http://www.ruby-forum.com/\.