Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Finding an element in an array of hashes

Reply
Thread Tools

Finding an element in an array of hashes

 
 
Alex Stupakov
Guest
Posts: n/a
 
      12-04-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Aldric Giacomoni
Guest
Posts: n/a
 
      12-04-2009
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/Enu...e.html#M003122
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Alex Stupakov
Guest
Posts: n/a
 
      12-04-2009
Aldric Giacomoni 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 }
>
> http://ruby-doc.org/core/classes/Enu...e.html#M003122

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      12-04-2009
On Fri, Dec 4, 2009 at 9:37 PM, Aldric Giacomoni <> wrote:
> 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 }


Careful, there should be a == there...

Jesus.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array of Hashes in an array of hashes - Complicated! Matt Brooks Ruby 16 09-16-2009 05:53 PM
array of hashes (finding value) Nate Leavitt Ruby 5 07-14-2008 10:43 PM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
array of hashes: finding hash with min value for key Patrick Cotner Ruby 3 09-04-2007 03:00 AM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57