I really don't understand the difference between shallow and deep copy. Ruby's
Produces a shallow copy of obj---the instance variables of obj are
copied, but not the objects they reference.
a = Klass.new
a.name = "John"
b = a.dup
b.name = "Sue"
puts a.name # John
objects they reference
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 end anna = Klass.new anna.name = 'Anna' anna_lisa = anna.dup anna_lisa.name << ' Lisa' # => "Anna Lisa" anna.name # => "Anna Lisa"
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.
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.