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

317
Views
Element Replace - Ruby

I am trying to create a new array where elements of the original array are replaced with their corresponding values in the hash. I want to compare every element in arr to the key in hash and if they are equal shovel them into the arr and return it at the end. Why is my code not working and how can I access/return the key value of the respective entry in hash, not only the value pointed to by the key? If you get what I am saying.

def element_replace(arr, hash)
  count = []
    
  
  for i in arr do
    if i == hash.key
     count << value
    else 
      count << i 
    end
  end
   
  return count

end

arr1 = ["LeBron James", "Lionel Messi", "Serena Williams"]
hash1 = {"Serena Williams"=>"tennis", "LeBron James"=>"basketball"}
print element_replace(arr1, hash1) # => ["basketball", "Lionel Messi", "tennis"]
puts

arr2 = ["dog", "cat", "mouse"]
hash2 = {"dog"=>"bork", "cat"=>"meow", "duck"=>"quack"}
print element_replace(arr2, hash2) # => ["bork", "meow", "mouse"]
puts
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Ruby's Hash::fetch would be a technique to get your desired result:

> players = ["LeBron James", "Lionel Messi", "Serena Williams"]
=> ["LeBron James", "Lionel Messi", "Serena Williams"]
> sports_data = {"Serena Williams"=>"tennis", "LeBron James"=>"basketball"}
=> {"Serena Williams"=>"tennis", "LeBron James"=>"basketball"}
> players.map { |player| sports_data.fetch(player, player) }
=> ["basketball", "Lionel Messi", "tennis"]
over 4 years ago · Santiago Trujillo Report

0

You can map the array to hash keys or keep the array element itself: if the key is missing (return nil) keep the array element:

arr1.map { |k| hash1[k] || k }
#=> ["basketball", "Lionel Messi", "tennis"]
over 4 years ago · Santiago Trujillo Report

0

If your hash is supposed to return the key by default, you could use a default_proc:

hash = { 'Serena Williams' => 'tennis', 'LeBron James' => 'basketball' }
hash.default_proc = proc { |hash, key| key }

Which gives you:

hash['LeBron James'] #=> "basketball"    # explicit value
hash['Lionel Messi'] #=> "Lionel Messi"  # default value

or, using an array:

arr = ['LeBron James', 'Lionel Messi', 'Serena Williams']

hash.values_at(*arr)
#=> ["basketball", "Lionel Messi", "tennis"]
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!