# How to use include? on an array of objects

**URL:** <https://www.ruby-forum.com/t/how-to-use-include-on-an-array-of-objects/153838>\
**Category:** Ruby\
**Created:** [November 21, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/how-to-use-include-on-an-array-of-objects/153838 "2008-11-21T18:48:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 21, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/how-to-use-include-on-an-array-of-objects/153838/1 "2008-11-21T18:48:49Z")

</div>

I have the following array:

[product1, product2, product3]

All products are of type Product. The Product class defines isntance  
attributes such as: title, description, price.

Can I use include? to check if the array has a product which price is  
zero? if not what would I need to do? Iterate over each product and set  
a variable if a match is found?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 21, 2008, 7:02pm UTC](https://www.ruby-forum.com/t/how-to-use-include-on-an-array-of-objects/153838/2 "2008-11-21T19:02:11Z")

</div>

Use the ‘select’ method of array  
[http://ruby-doc.org/core/classes/Array.html#M002214](http://ruby-doc.org/core/classes/Array.html#M002214)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 21, 2008, 7:02pm UTC](https://www.ruby-forum.com/t/how-to-use-include-on-an-array-of-objects/153838/3 "2008-11-21T19:02:47Z")

</div>

> I have the following array:
> 
> [product1, product2, product3]
> 
> All products are of type Product. The Product class defines isntance  
> attributes such as: title, description, price.
> 
> Can I use include? to check if the array has a product which price is  
> zero? if not what would I need to do?

You probably want any? :

product\_array.any? { |p| p.price.zero? }

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 21, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/how-to-use-include-on-an-array-of-objects/153838/4 "2008-11-21T19:09:16Z")

</div>

On Nov 21, 2008, at 11:44 AM, Fernando P. wrote:

> a variable if a match is found?  
> Given arr = [product1, product2, product3], and you have a ‘price’  
> method on your products:

If you want to find the first item of price zero:

```
arr.find { |prod| prod.price.zero? }

```

If you want to find all items of price zero:

```
arr.select { |prod| prod.price.zero? }

```

If you just want to know that at least one item has zero price:

```
arr.any? { |prod| prod.price.zero? }
```

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 21, 2008, 8:57pm UTC](https://www.ruby-forum.com/t/how-to-use-include-on-an-array-of-objects/153838/5 "2008-11-21T20:57:54Z")

</div>

On Nov 21, 2008, at 09:44 , Fernando P. wrote:

> a variable if a match is found?  
> c’mon guys… teach a man to fish.

Fernando, check out ri. ri is your friend™.

% ri Array  
% ri Enumerable

(Array includes Enumerable). There is a LOT of good stuff in  
Enumerable. You should poke around and learn as much of it as you can:

% ri Enumerable.find  
% ri Enumerable.find\_all

etc.

Other places to look are [http://ruby-doc.org/](http://ruby-doc.org/) and the PragProg pickaxe  
book (Programming Ruby, 2nd ed).
