Finding an element in an array of hashes

Discussion in 'Ruby' started by Alex Stupakov, Dec 4, 2009.

  1. 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/.
    Alex Stupakov, Dec 4, 2009
    #1
    1. Advertising

  2. 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
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Dec 4, 2009
    #2
    1. Advertising

  3. Alex Stupakov, Dec 4, 2009
    #3
  4. 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.
    Jesús Gabriel y Galán, Dec 4, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kazaam
    Replies:
    12
    Views:
    262
    Matthias Wächter
    Sep 13, 2007
  2. Patrick Cotner
    Replies:
    3
    Views:
    94
    Patrick Cotner
    Sep 4, 2007
  3. Nate Leavitt

    array of hashes (finding value)

    Nate Leavitt, Jul 14, 2008, in forum: Ruby
    Replies:
    5
    Views:
    110
    Nate Leavitt
    Jul 14, 2008
  4. Matt Brooks
    Replies:
    16
    Views:
    220
    Matt Brooks
    Sep 16, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    201
Loading...

Share This Page