All,
I was wondering if anyone knew a better patten than:
array_of_hashes.map { |hash_from_array| hash_from_array[:key] }
for retrieving an array of values with a specific key from an array of hashes containing that key.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From the Ruby code perspective, the
mapis pretty elegant and straightforward.From the algorithmic point of view (to address the computer-science tag), it seems a solution to this problem cannot be better than going through the whole array once (i.e. a
maphere), so it will take as much time as to process each hash in the array.@Vlad: Compacting the returned array depends on what will be done with the array, right? 🙂