Marko Avlijaš Marko Avlijaš - 6 months ago 24
Ruby Question

Provide simplest example where deep copy is needed in ruby

I really don't understand the difference between shallow and deep copy. Ruby's

seems to create a deep copy when I test it.

Documentation says:

Produces a shallow copy of obj---the instance variables of obj are
copied, but not the objects they reference.

But when I test this it seems to change the objects they reference.

class Klass
attr_accessor :name

a = = "John"
b = a.dup = "Sue"
puts # John

Why is shallow copy sufficient here when
is one of
objects they reference

What's the simplest example where deep copy is needed?


The example you have shown does not describe the difference between a deep and a shallow copy. Instead, consider this example:

class Klass
  attr_accessor :name

anna = = 'Anna'

anna_lisa = anna.dup << ' Lisa'
# => "Anna Lisa"
# => "Anna Lisa"

Generally, dup and clone are both expected to just duplicate the actual object you are calling the method on. No other referenced objects like the name String in the above example are duplicated. Thus, after the duplication, both, the original and the duplicated object point to the very same name string.

With a deep_dup, typically all (relevant) referenced objects are duplicated too, often to an infinite depth. Since this is rather hard to achieve for all possible object references, often people rely on implementation for specific objects like hashes and arrays.

A common workaround for a rather generic deep-dup is to use Ruby's Marshal class to serialize an object graph and directly unserializing it again.

anna_lena = Marshal.load( Marshal.dump(anna))

This creates new objects and is effectively a deep_dup. Since most objects support marshaling right away, this is a rather powerful mechanism. Note though than you should never unmarshal (i.e. load) user-provided data since this will lead to a remote-code execution vulnerability.