# Find\_all help

**URL:** <https://www.ruby-forum.com/t/find-all-help/68374>\
**Category:** Ruby\
**Created:** [September 12, 2006, 6:21am UTC](https://www.ruby-forum.com/t/find-all-help/68374 "2006-09-12T06:21:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kovax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7ab992/32.png) [@kovax](https://www.ruby-forum.com/u/kovax)\
**Post date:** [September 12, 2006, 6:21am UTC](https://www.ruby-forum.com/t/find-all-help/68374/1 "2006-09-12T06:21:54Z")

</div>

Hello,

I have an array filled with employee objects that are defined like  
this:

Employee = Struct.new(:name, :age)

I would like to use the find\_all method on an array of employee objects  
to find all employee objects with an age \> 18. I tried the following  
code but I was unsuccessful:

employees = []  
employees.add Employee.new(“joe” , 4)  
employees.add Employee.new(“sam” , 20)

employees.select { Employee.age \> 18 }

Any help or pushes in the right direction would be really appreciated.

---

<div class="post-metadata">

**Author:** ![kovax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7ab992/32.png) [@kovax](https://www.ruby-forum.com/u/kovax)\
**Post date:** [September 12, 2006, 6:35am UTC](https://www.ruby-forum.com/t/find-all-help/68374/2 "2006-09-12T06:35:57Z")

</div>

kovax wrote:

> employees = []  
> employees.add Employee.new(“joe” , 4)  
> employees.add Employee.new(“sam” , 20)
> 
> employees.select { Employee.age \> 18 }
> 
> Any help or pushes in the right direction would be really appreciated.

Hi there,

In the block to select you don’t want to use the Employee object, you  
want to use the instances you created of it, which will be passed in  
through the block argument. Also, I don’t know if add is a custom  
method you made, or mabye an alias for push or \<\<, or if you are asking  
about that too, so in this code I use push:

Employee = Struct.new(:name, :age)  
employees = []  
employees.push Employee.new(“joe” , 4)  
employees.push Employee.new(“sam” , 20)  
adults = employees.select { |e| e.age \> 18 }  
adults.each { |e| puts “#{e.name}, #{e.age}” }

# =\> sam, 20

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![kovax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7ab992/32.png) [@kovax](https://www.ruby-forum.com/u/kovax)\
**Post date:** [September 12, 2006, 6:59am UTC](https://www.ruby-forum.com/t/find-all-help/68374/3 "2006-09-12T06:59:59Z")

</div>

On Sep 12, 2006, at 12:20 AM, kovax wrote:

> code but I was unsuccessful:
> 
> employees = []  
> employees.add Employee.new(“joe” , 4)  
> employees.add Employee.new(“sam” , 20)
> 
> employees.select { Employee.age \> 18 }
> 
> Any help or pushes in the right direction would be really appreciated.

I was tempted to answer that your code is illegal because employing  
four-year olds is illegal :), but the real answer is Array doesn’t  
support ‘add’. In Ruby you use ‘push’ or ‘\<\<’. Further, the form for  
using ‘select’ is:

```
a_collection.select { |identifier| <code referring to identifier> }

```

where ‘identifier’ gets bound successively to elements of  
‘a\_collection’. So try something like:

`
#! /usr/bin/ruby -w
Employee = Struct.new(:name, :age)
employees = []
employees << Employee.new(“joe” , 4)
employees << Employee.new(“sam” , 20)
p employees.select {|e| e.age > 18 }
` [#]

Regards, Morton
