I write down the hash:

hash[key.to_s.strip] = value.to_s.strip 

in hash:

 table['Order information'] = hash 

When cleaning hash :

 hash.clear 

it is cleared both by itself and in the hash table .
How do I avoid cleaning hash in a table while clearing hash itself

    1 answer 1

    Obviously, it must be another object.

    Now it is the same hashmap. Anyone who remembers the link to it will see the changes occurring in it too. Therefore, if you want to protect an object stored inside a large hash from accidental changes, save a copy there:

     table['Order information'] = hash.dup 

    ... and to guarantee you can freeze the object, losing the ability to make changes to it:

     table['Order information'] = hash.dup.freeze 

    Note that copying is superficial : the state of only the directly copied object is duplicated, but not of the objects to which it refers inside. It behaves like this:

     foo = {a: "ha-ha-ha"} bar = foo.dup # строка не является частью хэшмапа, dup скопировал лишь ссылку на неё: # если изменить строку в одном месте, она изменится и в другом foo[:a].tr!('a', 'o') foo # => {a: "ho-ho-ho"} # изменился, согласно строчке выше bar # => {a: "ho-ho-ho"} # тоже изменился, внезапно # но какие ключи хранятся в хэшмапе, он знает сам, непосредственно foo.delete(:a) foo # => {} bar # => {a: "ho-ho-ho"}