Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

318
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda