# Checking an array for an item

**URL:** <https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013>\
**Category:** Ruby\
**Created:** [January 4, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013 "2008-01-04T16:05:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 4, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013/1 "2008-01-04T16:05:08Z")

</div>

How come this code doesn’t return “true” for the following?

x = [1,2,3]

puts(x.include?(Fixnum))

Thanks

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 4, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013/2 "2008-01-04T16:18:10Z")

</div>

On Jan 4, 2008 9:05 AM, Sam P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How come this code doesn’t return “true” for the following?
> 
> x = [1,2,3]
> 
> puts(x.include?(Fixnum))

It uses ==, not ===.

$ri Enumerable#include?

---------------------------------------------------- Enumerable#include?  
enum.include?(obj) =\> true or false  
enum.member?(obj) =\> true or false

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 4, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013/3 "2008-01-04T16:45:06Z")

</div>

On Jan 4, 2008 4:05 PM, Sam P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How come this code doesn’t return “true” for the following?
> 
> x = [1,2,3]
> 
> puts(x.include?(Fixnum))

You could try something like:

class Array  
def include\_class?(klass)  
if self.detect { |x| x.class == klass }  
return true  
else  
return false  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 4, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013/4 "2008-01-04T16:20:30Z")

</div>

Sam P. wrote:

> How come this code doesn’t return “true” for the following?
> 
> x = [1,2,3]
> 
> puts(x.include?(Fixnum))
> 
> Thanks

Because include? doesn’t test the class of an object, it tests for the  
presence of an object. The ri command is your friend here:

## $ 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
```

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 4, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013/5 "2008-01-04T17:28:02Z")

</div>

> How come this code doesn’t return “true” for the following?
> 
> x = [1,2,3]
> 
> puts(x.include?(Fixnum))

In addition to the reasons other people mentioned, Fixnum is an actual  
object in Ruby. You could put it in an array if you wanted:

classes\_that\_start\_with\_f = [Fixnum, File, FizzBuzz]

classes\_that\_start\_with\_f.include?(Fixnum) # returns true

That’s why it doesn’t work like you expect; because #include?(Fixnum)  
is for finding the actual Fixnum class object.

To get what you want the best approach is map, or one of a few  
functions similar to map, such as collect or inject:

> > [1,2,3].map {|number| number.is\_a? Fixnum}  
> > =\> [true, true, true]  
> > [1,2,3,“asdfasdfasdf”].map {|number| number.is\_a? Fixnum}  
> > =\> [true, true, true, false]  
> > [1,2,3,“asdfasdfasdf”].map {|number| number.is\_a? Fixnum}.include?(true)  
> > =\> true

class Array  
def include\_class?(klass)  
map do |thing|  
thing.is\_a? klass  
end.include?(true)  
end  
end

> > [1,2,3].include\_class?(Fixnum)  
> > =\> true

Thomas W.'s solution is good too. #detect is a cousin of #map.

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 4, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013/6 "2008-01-04T21:12:45Z")

</div>

On Jan 4, 2008 11:25 AM, Giles B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > How come this code doesn’t return “true” for the following?
> > 
> > x = [1,2,3]
> > 
> > puts(x.include?(Fixnum))

> To get what you want the best approach is map, or one of a few  
> functions similar to map, such as collect or inject:

Well, if you’re trying to find out if the array contains at least one  
Fixnum then this isn’t really the best way. Instead I’d use

```
  x.any? {|obj| Fixnum === obj }

```

which returns a single boolean, and only looks at as many elements of  
the array as it needs to.

Alternately one could use

```
 x.find {|obj| Fixnum === obj }

```

which will return either the first Fixnum in the array, or nil if  
their isn’t one, which can be used the same way as a boolean value in  
almost all cases.

–  
Rick DeNatale

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

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 4, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013/7 "2008-01-04T23:10:03Z")

</div>

> Well, if you’re trying to find out if the array contains at least one  
> Fixnum then this isn’t really the best way. Instead I’d use
> 
> ```
> x.any? {|obj| Fixnum === obj }
> 
> ```
> 
> which returns a single boolean, and only looks at as many elements of  
> the array as it needs to.

Yeah, I think that’s the best way too.

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 4, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013/8 "2008-01-04T23:11:48Z")

</div>

> > Well, if you’re trying to find out if the array contains at least one  
> > Fixnum then this isn’t really the best way. Instead I’d use
> > 
> > ```
> > x.any? {|obj| Fixnum === obj }
> > 
> > ```
> > 
> > which returns a single boolean, and only looks at as many elements of  
> > the array as it needs to.
> 
> Yeah, I think that’s the best way too.

Wait, no I don’t. I’d say switch === with .is\_a? and that would be the  
best way. More readable, and allows you to avoid the question “what is  
===?”

Generally I think you should avoid === unless implementing it on new  
classes for the sake of case/switch statements.

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 4, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/checking-an-array-for-an-item/125013/9 "2008-01-04T20:50:58Z")

</div>

Giles B. wrote:

> > > [1,2,3].map {|number| number.is\_a? Fixnum}  
> > > =\> [true, true, true]

Thanks
