0

I have the following array where I want to select each of the keys:

names = [
 {"Ánias"=>{:gender=>"male", :nationality=>"faroese"}},
 {"Annfinnur"=>{:gender=>"male", :nationality=>"faroese"}},
 {"Ansgar"=>{:gender=>"male", :nationality=>"faroese"}}
]

How would I go about selecting all the names ("Ánias", "Annfinnur", "Ansgar")?

1
  • 1
    Do those hashes have always one key/value? (in that case it makes no sense to use a hash). Commented Mar 22, 2014 at 21:41

1 Answer 1

2

Just do

names = [
          {"Ánias"=>{:gender=>"male", :nationality=>"faroese"}},
          {"Annfinnur"=>{:gender=>"male", :nationality=>"faroese"}},
          {"Ansgar"=>{:gender=>"male", :nationality=>"faroese"}}
        ]

names.map { |h| h.keys.first }
# => ["Ánias", "Annfinnur", "Ansgar"]
Sign up to request clarification or add additional context in comments.

Comments

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.