mercredi 1 décembre 2021

Why all values of a nested hash are updated when merging another hash?

I have following hash:

hash = {2021=>{:gastosCompra=>0}, 2022=>{:gastosCompra=>0}, 2023=>{:gastosCompra=>0}}

I am trying to update gastosCompra of key year 2021. I've tried merge, deep_merge, but unfortunately all the other keys are updated too:

hash[year][:gastosCompra] = 555
hash[year].merge!({gastosCompra: 555})
hash[year].deep_merge!({gastosCompra: 555})

This is the result:

hash = {2021=>{:gastosCompra=>555}, 2022=>{:gastosCompra=>555}, 2023=>{:gastosCompra=>555}}

Why all the nested hashes are updated if I just want to update year 2021? I guess I could loop over the keys and stop when I find the year 2021, but I want to avoid that.

Aucun commentaire:

Enregistrer un commentaire