Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I break up and extract an array from an array-of-hashes in Ruby on Rails?

for example:

[ (id=>1, email=>'[email protected]', name=>'tim'),
  (id=>2, email=>'[email protected]', name=>'joe'),
  (id=>3, email=>'[email protected]', name=>'dan') ]

How can I extract the email column and put it in its own array?

like image 524
Tim Avatar asked Sep 30 '10 06:09

Tim


1 Answers

Let's call your array users. You can do this:

users.map{|u| u[:email]}

This looks at the hashes one by one, calling them u, extracts the :email key, and returns the results in a new array of user emails.

like image 157
Peter Avatar answered Nov 16 '22 04:11

Peter