Read more

Using #deep_dup for copying whole hashes and array

Jakob Scholz
April 22, 2020Software engineer at makandra GmbH

"Everything in Ruby is an object". This is also true for nested hashes and arrays. If you copy a hash with #clone or #dup and you modify the copy, you will run into the following behavior:

original_hash = { foo: { bar: 'original value' } }
copied_hash = original_hash.dup
copied_hash[:foo][:bar] = 'changed value'

original_hash # => { foo: { bar: "changed value" }
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

This is, because { bar: 'baz' } is an object, which is referenced in :foo. The copy of original_hash still holds the reference to the same object, so altering the value will also affect the copied hash.
This will get more obvious, if you are having references to e.g. ActiveRecord objects.

But in most cases, if you want to copy a hash, you want to get a copy of the whole structure. And this is where #deep_dup comes into play:

original_hash = { foo: { bar: 'original value' } }
copied_hash = original_hash.deep_dup
copied_hash[:foo][:bar] = 'changed value'

original_hash # => { foo: { bar: "original value" }

How it works:

#deep_dup simply calls itself recursively for arrays and hashes and calls #dup for all other Objects where #duplicable? is true.

Be careful using #deep_dup

Don't use #deep_dup when you are not sure about cyclic dependencies, because you will run into an endless loop:

a = {}
b = {a: a}
a[:b] = b
a.deep_dup # SystemStackError: stack level too deep
Posted by Jakob Scholz to makandra dev (2020-04-22 12:33)