2

I have an array of hashes:

[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}]

How can I convert it to array of values:

["male", "male", "female"]

6 Answers 6

4

In this case,

[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map(&:values).flatten

should work.

It takes an array from each hash, then flatten the nested array.

Sign up to request clarification or add additional context in comments.

Comments

4
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].flat_map(&:values)

Comments

2

A generic approach to this that would take into account other possible keys:

list = [{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}]

# Collect the 'sex' key of each hash item in the list.
sexes = list.collect { |e| e['sex'] }

1 Comment

I like this approach in case there are more keys than just the desired one. +1
2
arr = [{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}]
arr.map(&:values).flatten

EDIT: As directed by @tadman. Thanks!

3 Comments

There's no need to in-place flatten on an intermediate value. flatten will do the job.
@tadman Right. :) I just considered it, incase AdamNYC wished to retain them in the same array.
That flatten! call only affects the result of the map operation, and does not modify arr.
1

You can "map" the elements inside array, take the values of hashes, them "flatten" the resultant array.

[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map{|h| h.values }
=> [["male"], ["male"], ["female"]]

[["male"], ["male"], ["female"]].flatten
=> ["male", "male", "female"]

In a single line, you can:

[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map{|h| h.values }.flatten

or:

[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map(&:values).flatten

Docs:

4 Comments

-1 for posing an answer 12 minutes after an answer with the same solution was posted.
It is more thorough, but this exact solution has already been posted three times. Unlike being fashionably late to the party, this is just redundant.
Sorry, I don't knew that it was a race. Should I write lighting fast for earn some points the next time? I was trying to give a good answer, that include search something else. Sorry if I'm not fast enough.
It's not a race per se, it's a contest to provide the best answer, which is measured in efficiency and clarity, basically coming down the most Ruby-like often. Of course, being first with that helps, but often someone will post much later, with a more thought-out and comprehensive answer, and win the day. Some OPs grab the first answer without any knowledge of what would make the best answer, so you'll see a negative score for the selected answer, and another answer with a very high positive score.
-2
    arr = Array.new 
    my_hash.each do |el|
      #collect them here...
      arr.push(el["sex"]);
    end

Hope it helps

3 Comments

This doesn't transform, it just prints. You've left out the critical part of this. Not really an answer, is it?
I did write where he would collect them... so here it is corrected Thank you
There are several other answers which provide this functionality. This non-answer should be removed.

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.