Retrieving multiple key value attributes from an array

I have an array similar to the one you see at the bottom of this post (in particular, this is a list of fb friends returned from fb_graph).

If I wanted to go through this hash and select only the identifier attribute from each record, I know I could do something like this:

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

My question is if I want to highlight several attributes from each record. Basically I want to capture the id, name and photo. Naively, I want to do something like:

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

But of course this will not work. Any idea how I can do this?

 [#<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 
+4
source share
2 answers

This is not like a nested hash. It looks like an array of FbGraph :: User instances. If you want to turn it into an array of hashes, each of which contains: identifier ,: name and: picture, you can do this:

 @fb_friends.map { |f| { identifier: f.identifier, name: f.name, picture: f.picture } } 
+11
source

IIRC, this will return an hash array, where the key will be the id ad value of the array with the name and image:

 @fb_friends.map{|fbf| [fbf.identifier, [fbf.name, fbf.picture]]}.map{|a| Hash[a]} {:id1 => ['name', 'picture_url'], :id2 => ['name2', 'picture_url2]} 
+1
source

All Articles