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

138
Views
How to iterate over an array of hashes in Ruby and return all of the values of a specific key in a string

I'm currently trying to iterate over an array of hashes, and return all of the values of the "name" key in a string. Here's the array:

foods = 

  [
    { name: 'Dan Dan Noodles', cuisine: 'Sichuan', heat_level: 8 },
    { name: 'Nashville Hot Chicken', cuisine: 'American', heat_level: 7 },
    { name: 'Panang Curry', cuisine: 'Thai', heat_level: 4 },
  ]

Here's what I'm currently doing, and I'm not totally sure why it's not working!

  foods.each do |food|
    food.each do |k, v|
      if food == :name
        "#{v}"
      end
    end
  end

Thanks in advance.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use Enumerable#map for this:

p foods.map { |f| f[:name] }

The code you tried to use did not produce any output or create any objects, and it was not necessary to use a second loop to access a single element of a hash.

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!