# Array checking?

**URL:** <https://www.ruby-forum.com/t/array-checking/120256>\
**Category:** Ruby\
**Created:** [November 15, 2007, 10:59am UTC](https://www.ruby-forum.com/t/array-checking/120256 "2007-11-15T10:59:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 15, 2007, 10:59am UTC](https://www.ruby-forum.com/t/array-checking/120256/1 "2007-11-15T10:59:09Z")

</div>

hi

i have written a array like this.

my\_array = [“one”,“two”,“three”,“four”]

no i want to check a given string is can be found from the above array.

eg:

```
     if ( my_array.map{"one"} )
       return true;
     else
      return false;
      end

```

so here if i use “five” insted of “one” it should return false( as i  
think) but it does no happen.

(simply want to check a given string is in the array or not)

so if any body can help???

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 15, 2007, 11:07am UTC](https://www.ruby-forum.com/t/array-checking/120256/2 "2007-11-15T11:07:43Z")

</div>

Nadeesha M. wrote:

> (simply want to check a given string is in the array or not)

if my\_array.include?(“one”)

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 15, 2007, 11:10am UTC](https://www.ruby-forum.com/t/array-checking/120256/3 "2007-11-15T11:10:42Z")

</div>

-------- Original-Nachricht --------

> Datum: Thu, 15 Nov 2007 18:59:07 +0900  
> Von: Nadeesha M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: Array checking ???

> eg:
> 
> (simply want to check a given string is in the array or not)
> 
> ## so if any body can help???
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Dear Nadeesha,

you can use

class Array  
def contains\_entry?(entry)  
if self-[entry]==self  
return false  
else  
return true  
end  
end  
end

a=[“one”,“two”,3,4]  
p a.contains\_entry?(“three”)

The idea is to see whether the array stays the same when you take  
away something from it - if yes, that something wasn’t in the array  
in the first place.

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 15, 2007, 11:52am UTC](https://www.ruby-forum.com/t/array-checking/120256/4 "2007-11-15T11:52:55Z")

</div>

Hi –

On Thu, 15 Nov 2007, Axel E. wrote:

> > ```
> > else
> > 
> > ```
> > 
> > –  
> > else  
> > in the first place.  
> > No, you definitely don’t want to do that, for many reasons. As Jari  
> > said, just use include?

if array.include?(entry)

David

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 15, 2007, 11:34am UTC](https://www.ruby-forum.com/t/array-checking/120256/5 "2007-11-15T11:34:55Z")

</div>

Axel E. wrote:

> -------- Original-Nachricht --------
> 
> > Datum: Thu, 15 Nov 2007 18:59:07 +0900  
> > Von: Nadeesha M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > An: removed\_email\_address@domain.invalid  
> > Betreff: Array checking ???
> 
> > eg:
> > 
> > (simply want to check a given string is in the array or not)
> > 
> > ## so if any body can help???
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Dear Nadeesha,
> 
> you can use
> 
> class Array  
> def contains\_entry?(entry)  
> if self-[entry]==self  
> return false  
> else  
> return true  
> end  
> end  
> end
> 
> a=[“one”,“two”,3,4]  
> p a.contains\_entry?(“three”)
> 
> The idea is to see whether the array stays the same when you take  
> away something from it - if yes, that something wasn’t in the array  
> in the first place.
> 
> Best regards,
> 
> Axel

THANKS A LOT PAL

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 15, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/array-checking/120256/6 "2007-11-15T12:30:28Z")

</div>

On Nov 15, 2007 5:59 PM, Nadeesha M. \> my\_array =  
[“one”,“two”,“three”,“four”]

> no i want to check a given string is can be found from the above array.

many ways

try,

~\> my\_array.index “five”  
=\> nil

~\> my\_array.include? “five”  
=\> false

~\> my\_array.any?{|e| e== “five”}  
=\> false

i like index since it’s fast and it also returns the position if  
found. wish include? would do that too.

anyway, all of those meth above has its unique advantage though. eg,  
with any?, you can do fancy search…

kind regards -botp

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 15, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/array-checking/120256/7 "2007-11-15T16:58:39Z")

</div>

On Nov 15, 2007 4:59 AM, Nadeesha M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> eg:
> 
> (simply want to check a given string is in the array or not)
> 
> so if any body can help???

Others have told you what works. Let’s talk about why this doesn’t.  
What does the map method do? Here’s the ri doc

## array.map {|item| block } → an\_array

Invokes _block_ once for each element of _self_. Creates a new  
array containing the values returned by the block

Normally you have an argument in the block attached to map, to receive  
each element of the array, you didn’t but Ruby let’s you get away with  
that.

Now let’ run your code and see what the map returns:

my\_array = [“one”,“two”,“three”,“four”]

my\_array.map{“one”} # =\> [“one”, “one”, “one”, “one”]

So it’s just an array with the same number of elements, each with a  
value of “one”.

And since it’s neither false, nor nil, this will always be treated as  
true in a boolean context.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![nad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ed8c4c/32.png) [@nad](https://www.ruby-forum.com/u/nad)\
**Post date:** [November 15, 2007, 12:26pm UTC](https://www.ruby-forum.com/t/array-checking/120256/8 "2007-11-15T12:26:22Z")

</div>

-------- Original-Nachricht --------

> Datum: Thu, 15 Nov 2007 19:51:58 +0900  
> Von: “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: Re: Array checking ???

> > > eg:
> > 
> > class Array  
> > p a.contains\_entry?(“three”)
> 
> David
> 
> –  
> Upcoming training by David A. Black/Ruby Power and Light, LLC:
> 
> - Advancing With Rails, Berlin, Germany, November 19-22
> - Intro to Rails, London, UK, December 3-6 (by Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details!

Dear Nadeesha,

indeed, Jari’s solution is way better. Shorter, more elegant.  
I wouldn’t have written  
a post had Jari’s response already been on the list.

Best regards,

Axel
