On 10/16/2009 09:45 PM, Rajinder Yadav wrote:
> Robert Klemme wrote:
>> 2009/10/16 lith <minilith / gmail.com>:
>>>> But I think the spirit of dup
>>>> described above is that dup defined in a subclass should initialize it
>>>> using its constructor.
>> Brian, I disagree.  The proper way is to implement #initialize_copy.
>> That way you can make sure you do not get aliasing effects even if
>> source and copy are frozen because in #initialize_copy frozen state is
>> not applied.
>>
>>> I'd understand the description in such a way that user should
>>> override
>>> neither #dup not #clone but instead create a #initialize_copy method
>>> to
>>> implement anything class-specific (including a non-shallow copy).
>> Also for shallow copy in order to avoid aliasing!  IMHO a proper setup
>> looks like this:
> 
> Robert, I like this setup, thanks for the sample code to look over, just 
> discovered why adding 'super' is important, which was missing from my notes and 
> an oversight on my part.
> 
> It is sufficient to call 'super' and not 'super source'? if you are passing 
> stuff up the hierarchy construction chain.

You seem to be mixing two things: super in #initialize and 
#initialize_copy.  In #initialize_copy you can simply write "super" 
(without brackets) because that will make sure the argument list is 
propagated.  You can do this because #initialize_copy will always only 
have one argument, the object that was duped / cloned.

In the constructor I explicitly wrote "super()" because the super class 
#initialize does not have arguments and "super" will break as soon as 
you add parameters to the sub class constructor.  Of course, if you 
change both classes in parallel you can stick with "super".

> I am going to conjecture 'super' ends up becoming 'super self', which make sense 
> because the parent constructor don't care about sub class data members. Does 
> that make any sense to you?

No.  Neither for #initialize nor for #initialize_copy you want self as 
argument to super.

Kind regards

	robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/