# Finding an element in an array of hashes

**URL:** <https://www.ruby-forum.com/t/finding-an-element-in-an-array-of-hashes/179829>\
**Category:** Ruby\
**Created:** [December 4, 2009, 9:15pm UTC](https://www.ruby-forum.com/t/finding-an-element-in-an-array-of-hashes/179829 "2009-12-04T21:15:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [December 4, 2009, 9:15pm UTC](https://www.ruby-forum.com/t/finding-an-element-in-an-array-of-hashes/179829/1 "2009-12-04T21:15:35Z")

</div>

Hi,

I have an array of Facebooker::User objects

so friends.class = Array  
and friends[1].class = Facebooker::User

Facebooker::User objects which are basically hashes.

each friend has a unique facebook\_id so  
friends[1].facebook.id.class = Integer

I’d like to find the array member with a particular facebook\_id.

friends.find\_by\_id(12345) should return the corresponding  
Facebooker::User object

What’s the cleanest way to do this?  
Do I have to iterate over the array members or is there a method in Ruby  
that does this?

Thanks

Alex

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [December 4, 2009, 9:37pm UTC](https://www.ruby-forum.com/t/finding-an-element-in-an-array-of-hashes/179829/2 "2009-12-04T21:37:39Z")

</div>

Alex Stupakov wrote:

> Hi,
> 
> I have an array of Facebooker::User objects  
> so friends.class =\> Array  
> and friends[1].class =\> Facebooker::User
> 
> Facebooker::User are objects which are basically hashes.  
> friends[1].facebook.id.class =\> Integer
> 
> I’d like to find the array member with a particular facebook\_id.  
> friends.find\_by\_id(12345) should return the corresponding  
> Facebooker::User object
> 
> What’s the cleanest way to do this?  
> Do I have to iterate over the array members or is there a method in Ruby  
> that does this?

“basically” hashes? That doesn’t look like hash syntax to me…

friends.find { |friend| friend.facebook.id = 12345 }

[http://ruby-doc.org/core/classes/Enumerable.html#M003122](http://ruby-doc.org/core/classes/Enumerable.html#M003122)

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [December 4, 2009, 11:22pm UTC](https://www.ruby-forum.com/t/finding-an-element-in-an-array-of-hashes/179829/3 "2009-12-04T23:22:02Z")

</div>

Aldric G. wrote:

> “basically” hashes? That doesn’t look like hash syntax to me…

You’re right, my mistake.  
Thanks for the suggestion, it works great, and for the link.

> friends.find { |friend| friend.facebook.id = 12345 }
> 
> [module Enumerable - RDoc Documentation](http://ruby-doc.org/core/classes/Enumerable.html#M003122)

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [December 5, 2009, 12:24am UTC](https://www.ruby-forum.com/t/finding-an-element-in-an-array-of-hashes/179829/4 "2009-12-05T00:24:29Z")

</div>

On Fri, Dec 4, 2009 at 9:37 PM, Aldric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > I’d like to find the array member with a particular facebook\_id.  
> > friends.find\_by\_id(12345) should return the corresponding  
> > Facebooker::User object
> > 
> > What’s the cleanest way to do this?  
> > Do I have to iterate over the array members or is there a method in Ruby  
> > that does this?
> 
> “basically” hashes? That doesn’t look like hash syntax to me…
> 
> friends.find { |friend| friend.facebook.id = 12345 }

Careful, there should be a == there…

Jesus.
