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

887
Views
Ruby on rails: store all specific key values from a hash into an array

How can I create an array of all "name_printed" keys in the following hash with nil safe navigation?

 {
  "show_data_on_file": "Yes",
  "employees_names_printed_on_badges": [
    {
      "name_printed": "John",
      "badge_id": 12160060
    },
    {
      "name_printed": "Tom",
      "badge_id": 12160080
    }
  ]
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could use Enumerable#pluck. In the case employees_names_printed_on_badges returns a falsy value, you invoke pluck on an empty array:

(data[:employees_names_printed_on_badges] || []).pluck(:name_printed)
# ["John", "Tom"]

Or use &, which would result in nil if data[:employees_names_printed_on_badges]& is nil.

data[:employees_names_printed_on_badges]&.pluck(:name_printed)
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!