# Search in array

**URL:** <https://www.ruby-forum.com/t/search-in-array/83202>\
**Category:** Ruby\
**Created:** [January 24, 2007, 9:35am UTC](https://www.ruby-forum.com/t/search-in-array/83202 "2007-01-24T09:35:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![materni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@materni](https://www.ruby-forum.com/u/materni)\
**Post date:** [January 24, 2007, 9:35am UTC](https://www.ruby-forum.com/t/search-in-array/83202/1 "2007-01-24T09:35:50Z")

</div>

hi all  
is there in ruby a method like in\_array() php function?

i need to verify if there is a string in an array

---

<div class="post-metadata">

**Author:** ![materni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@materni](https://www.ruby-forum.com/u/materni)\
**Post date:** [January 24, 2007, 9:46am UTC](https://www.ruby-forum.com/t/search-in-array/83202/2 "2007-01-24T09:46:51Z")

</div>

Alle 09:35, mercoledÃ¬ 24 gennaio 2007, Giuseppe M. ha scritto:

> hi all  
> is there in ruby a method like in\_array() php function?
> 
> i need to verify if there is a string in an array

I don’t know php, so I don’t know what in\_array() exactly does. At any  
rate,  
the method include? of the Array class tells whether a given element is  
in  
the array:

a=[‘a’, ‘b’, ‘c’]  
a.include?(‘a’)  
=\>true  
a.include?(‘d’)  
=\>false

This method uses the == method of the argument to test for equality. If  
you  
need something different, you can use the grep method, which uses ===  
and  
returns an array containing the matches (useful, for instance, for  
regular  
expressions), find, which takes a block and returns the first element  
for  
which the block returns true, or any? which works like find but returns  
a  
boolean value indicating whether there was a match:

a=[‘abc’,‘def’]  
a.include?(‘a’)  
=\>false  
a.grep(/a/)  
=\>[‘abc’]

a=[1,2,3]  
a.find{|i| i\>1}  
=\>2  
a.any?{|i| i\>1}  
=\>true

I hope this helps

---

<div class="post-metadata">

**Author:** ![materni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@materni](https://www.ruby-forum.com/u/materni)\
**Post date:** [January 24, 2007, 9:47am UTC](https://www.ruby-forum.com/t/search-in-array/83202/3 "2007-01-24T09:47:52Z")

</div>

On 1/24/07, Giuseppe M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hi all  
> is there in ruby a method like in\_array() php function?
> 
> i need to verify if there is a string in an array

array.include? string

## ri Array#include? --------------------------------------------------------- Array#include? array.include?(obj) → true or false

```
 Returns +true+ if the given object is present in _self_ (that is,
 if any object +==+ _anObject_), +false+ otherwise.

    a = ["a", "b", "c"]
    a.include?("b") #=> true
    a.include?("z") #=> false

```

martin

---

<div class="post-metadata">

**Author:** ![materni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@materni](https://www.ruby-forum.com/u/materni)\
**Post date:** [January 24, 2007, 9:50am UTC](https://www.ruby-forum.com/t/search-in-array/83202/4 "2007-01-24T09:50:17Z")

</div>

thank you very mutch 😉  
this is that i’m searching
