How can I merge two hashes without overwritten duplicate keys in Ruby?

前端 未结 5 790
不知归路
不知归路 2020-12-07 12:51

Is there an easy or elegant way to merge two hashes without overwriting duplicate keys?

That is, if the key is present in the original hash I don\'t want to

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 13:28

    There is a way in standard Ruby library to merge Hashes without overwriting existing values or reassigning the hash.

    important_hash.merge!(defaults) { |key, important, default| important }
    

提交回复
热议问题