I am trying to figure out how to perform a deep clone


class A
    attr_accessor :name
end

a1 = A.new
a1.name = "yoyoma"
a2 = a1.dup
a1.name.chop!
puts a2.name


I found the following way to write a deep clone method

class A
   attr_accessor :name
   def dup
      Marshal::load(Marshal.dump(self))
   end
end

If I wanted to write my own specialize deep_cloner, how would I do this. If I 
try the obvious way to do it,

def dup
    @name = self.name.dup
end

I get an error when 'puts a2.name' is executed saying:

NoMethodError: undefined method `name' for "yoyom":String


Can someone explain what's going on when dup is called. What gets passes to dup 
(i assume self) and why my code is wrong?


-- 
Kind Regards,
Rajinder Yadav

http://DevMentor.org
Do Good ~ Share Freely