Brian Candler wrote:
> Rajinder Yadav wrote:
>> 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?
> 
> dup is a method of your existing object, and should return the new 
> object instance.
> 
> class A
>   attr_accessor :name
>   def dup
>     res = self.class.new
>     res.name = name.dup
>     res
>   end
> end

Brain, I am starting to see where I went wrong, this clears it up, thanks!

> 
> a1 = A.new
> a1.name = "yoyoma"
> a2 = a1.dup
> a1.name.chop!
> puts a2.name
> 
> There is a subtle distinction between 'dup' and 'clone' which I'll leave 
> someone else to explain...


-- 
Kind Regards,
Rajinder Yadav

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