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

325
Views
How to return all even numbers inside an array inside of a hash?

I've been given the following data structure:

users = {
  "Jonathan" => {
    :twitter => "tronathan",
    :favorite_numbers => [12, 42, 75],
  },
  "Erik" => {
    :twitter => "sferik",
    :favorite_numbers => [8, 12, 24],
  },
  "Anil" => {
    :twitter => "bridgpal",
    :favorite_numbers => [12, 14, 85],
  },
}

I need to return all of Anils favourite numbers that are even. This is what I have so far:

users["Anil"][:favorite_numbers].each do |evennum|
    if evennum.even?
        puts evennum
    end
end
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could do something like this

anil_favorite_even_numbers = users['Anil'][:favorite_numbers].select(&:even?)

This takes for granted that a user Anil exists and the favourite_numbers inside it too and that's an array. Otherwise we need a little bit of extra work.

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!