4

I have an array like the one you see at the bottom of this post (specifically, it's a list of fb friends returned from fb_graph).

If I wanted to go through this hash and pick out just the 'identifier' attribute from each record, I know I could do something like this:

@fb_friends.map &:identifier  # returns ["123", "134",...]

My question is what if I want to pick out multiple attributes from each record. Basically I want to grab the identifier, name and picture. Naively I want to do something like:

@fb_friends.map &:identifier, :name, :picture

But of course that doesn't work. Any idea how I can pull this off?

[#<FbGraph::User:0x007f9ec1fd63c8 @identifier="1868", @endpoint="https://graph.facebook.com /----",  @access_token="AAAFGzU8vU0gBAEK2q3WYEUJNZAczuO6UPOLqbscwvZALr1gW1ePBqpyarh0uTv6alA2YyNvIzZBcvvIBEYy4i5ulMJZCpWcZD", @cached_collections={}, @name="REMOVED", @first_name=nil, @middle_name=nil, @last_name=nil, @gender=nil, @locale=nil, @languages=[], @link=nil, @username=nil, @third_party_id=nil, @timezone=nil, @verified=nil, @about=nil, @bio=nil, @education=[], @email=nil, @interested_in=[], @political=nil, @favorite_teams=[], @quotes=nil, @relationship_status=nil, @religion=nil, @relationship=nil, @website=nil, @work=[], @sports=[], @favorite_athletes=[], @inspirational_people=[], @mobile_phone=nil, @installed=nil>, #<FbGraph::User:0x007f9ec1fda9f0 @identifier="3??1", @endpoint="https://graph.facebook.com/----", @access_token="AAAFGzU8vU0gBAEK2q3WYEUJNZAczuO6UPOLqbscwvZALr1gW1ePBqpyarh0uTv6alA2YyNvIzZBcvvIBEYy4i5ulMJZCpWcZD", @cached_collections={}, @name="Removed", @first_name=nil, @middle_name=nil, @last_name=nil, @gender=nil, @locale=nil, @languages=[], @link=nil, @username=nil, @third_party_id=nil, @timezone=nil, @verified=nil, @about=nil, @bio=nil, @education=[], @email=nil, @interested_in=[], @political=nil, @favorite_teams=[], @quotes=nil, @relationship_status=nil, @religion=nil, @relationship=nil, @website=nil, @work=[], @sports=[], @favorite_athletes=[], @inspirational_people=[], @mobile_phone=nil, @installed=nil>, #<FbGraph::User:0x007f9ec1fe5030

2 Answers 2

11

That doesn't look like a nested hash. It looks like an array of FbGraph::User instances. If you'd like to turn it into an array of hashes, each containing :identifier, :name, and :picture, you could do this:

@fb_friends.map { |f|
  { identifier: f.identifier, name: f.name, picture: f.picture }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly. Thanks and I'll update the question to array.
1

IIRC this would return an array of hashes where the key would be the id ad the value an array with the name and picture:

@fb_friends.map{|fbf| [fbf.identifier, [fbf.name, fbf.picture]]}.map{|a| Hash[a]}

{:id1 => ['name', 'picture_url'], :id2 => ['name2', 'picture_url2]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.