views:

49

answers:

2

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?

+7  A: 

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.

Peter
You could also use users.collect{|u| u[:email]}, collect is an alias for map, which might be easier for some people (those who don't have a functional background).
Sven Koschnicke
awesome! this worked perfectly. thanks a bunch
Tim
+1  A: 
[ {id=>1, email=>'[email protected]', name=>'tim'},
  {id=>2, email=>'[email protected]', name=>'joe'},
  {id=>3, email=>'[email protected]', name=>'dan'} ].map{|h| h['email']}
shingara