Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Adding new keys to a Array Of Hashes

I have a collection of K:V Pairs as seen below:

list = [

{"Mary"=>"Die Hard"},
{"Paul"=>"The Grinch"},
{"John"=>"Halloween"},

]

But now I would like to flesh out and change the list, adding more specific keys in order to make it more searchable.

Is this possible or even an advisable way to go about this?

new_list = [

 { name: "Mary", film: "Die Hard"},
 { name: "Paul", film: "The Grinch"},
 { name: "John", film: "Halloween"},

]
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I would probably:

  1. Use map to iterate over the elements in list and build a new array at the same time
  2. Since each element is a hash with one item, destruct that into a key and value
  3. Build the new hash in the correct format
new_list = list.map do |hash|
  # e.g. key = "Mary", value = "Die Hard"
  key, value = hash.first

  {name: key, film: value}
end

This assumes that each hash in the list will have only one item (as you've shown in your example in the question) - you may want to validate this elsewhere, since this solution would ignore any other items in the hash past the first.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!