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

452
Vistas
Hash destructuring in Ruby 3

I Ruby 2 you could do the following:

my_hash = {a: {aa: 1, ab: 2, ac: 3}}

my_hash.each do |key, aa:, ab: 4, **|
  puts key
  puts aa
  puts ab
end

In Ruby 3 this now results in missing keywords :aa, :ab. What would be the best way to refactor code like this in Ruby 3?

Something like the following would not work because it doesn't support setting default values:

my_hash.each do |key, values|
  values in {aa: aa, ab: ab}
end

The best way I can think of is putting the existing code in a wrapper:

lambda = ->(key, aa:, ab: 4, **) do
  puts key
  puts aa
  puts ab
end

my_hash.each do |key, values|
  lambda.call(key, **values)
end

Any better options?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I can't think of a way to convert the hash to keyword arguments.

But why not use the hash the way it is, i.e. without treating its keys like keywords? Unless your actual code is more complex, fetch seems to do what you want:

my_hash = {a: {aa: 1, ab: 2, ac: 3}}

my_hash.each do |key, values|
  puts key
  puts values.fetch(:aa)
  puts values.fetch(:ab, 4)
end

The above works fine in both, Ruby 2 and 3. Just like your example, it will raise an error if :aa is missing and it will use a default value of 4 if :ab is missing.

over 4 years ago · Santiago Trujillo Denunciar

0

with ||

As stated by @Stefan it will not works if the value can contains nil or false

my_hash = {a: {aa: 1, ac: 3}}
 
my_hash.each do |key, values|
  puts key
  puts values[:aa]
  puts values[:ab] || 4
end

with key?

my_hash = {a: {aa: 1, ac: 3}}
default_values_hash = { :ab => 4 }
 
my_hash.each do |key, values|
  puts key
  puts values[:aa]
  puts values.key?(:ab) ? values[:ab] : default_values_hash[:ab]
end

with default

my_hash = {a: {aa: 1, ab: 2, ac: 3}}

my_hash.each do |key, values|
  values.default = :notfound
  puts key
  puts values[:aa]
  puts values[:ab] == :notfound ? 4 : values[:ab]
end
my_hash = {a: {aa: 1, ac: 3}}
 
my_hash.each do |key, values|
  values.default = :notfound
  puts key
  puts values[:aa]
  puts values[:ab] == :notfound ? 4 : values[:ab]
end

with default-proc

my_hash = {a: {aa: 1, ab: 2, ac: 3}}
default_values_hash = { :ab => 4 }

my_hash.each do |key, values|
  values.default_proc = proc { |hash, key| default_values_hash[key] || :notfound }
  puts key
  puts values[:aa]
  puts values[:ab]
end

with merge

my_hash = {a: {aa: 1, ac: 3}}
default_values_hash = { :ab => 4 }
 
my_hash.each do |key, values|
  values = default_values_hash.merge(values)
  puts key
  puts values[:aa]
  puts values[:ab]
end

All above code works in both Ruby 2 and 3

Note: I would suggest the key? method to test and check if key exists and handle the case accordingly as it would be much faster as comparing to other cases which require additional steps internally.

over 4 years ago · Santiago Trujillo Denunciar

0

We can assign each value to a variable and check if it is hash or not then we can print the values again in inner loop

my_hash = {a: { aa: 1, ab: 2, ac:3 }}
may_hash.each do |key, value|
  puts key
  if value.kind_of(Hash)
    value.each do |inner_key, inner_value|
      puts inner_key
      puts inner_value
    end
  else
    puts value
  end
end

In this we can refactor outer hash and inner hash at same time.

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