Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

319
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda