title: Ruby: object deep copying
body: <p>I'm looking at some techniques to deep-copy objects in Ruby (MRI 1.9.3).<br>
  I came across the following example, but I'm not sure about the <code>#dup</code> method implementation.
  I tested it and it does work, but I don't understand the logical steps of the method, thus I'm not confortable using it in my own code.  </p>
  
  <p>Is the statement <code>@name = @name.dup</code> referring to the iVar <strong>inside</strong> the copy? How? I can't see it.</p>
  
  <p>Could anyone explain it, please?<br>
  Also, are there better ways?</p>
  
  <pre><code>class MyClass
    attr_accessor :name
  
    def initialize(arg_str)   # called on MyClass.new("string")
      @name = arg_str         # initialize an instance variable
    end
  
    def dup
      the_copy = super        # shallow copy calling Object.dup
      @name = @name.dup       # new copy of istance variable
      return the_copy         # return the copied object
    end
  end
  </code></pre>