Forum: Ruby Newbie question about initialize methods in subclasses

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-05-10 19:09
All,

I just seem to have had a revelation about how the super keyword in Ruby
is ever so slightly different from the super keyword in Java.

In general, given

  class B < A

and desiring to add an initialize method to B, is it a "best practice"
to always do

  class B
  ...
  def initialize
    super
    ... custom B initialization here ...
  end

Basically, I'm trying to understand that if I instantiate B, then
A.initialize doesn't get called by default - is that correct?  Is that
so that B can completely override the initialization details of itself?

Thanks,
Wes
D4e51fd9554030ab55c379fdc1a34826?d=identicon&s=25 Keith Lancaster (klancaster)
on 2006-05-10 19:24
Wes Gamble wrote:
> All,
>
> I just seem to have had a revelation about how the super keyword in Ruby
> is ever so slightly different from the super keyword in Java.
>
> In general, given
>
>   class B < A
>
> and desiring to add an initialize method to B, is it a "best practice"
> to always do
>
>   class B
>   ...
>   def initialize
>     super
>     ... custom B initialization here ...
>   end
>
> Basically, I'm trying to understand that if I instantiate B, then
> A.initialize doesn't get called by default - is that correct?  Is that
> so that B can completely override the initialization details of itself?
>
> Thanks,
> Wes

A.intialize does not get called by default - you do have to call super.
This is not unique to the initialize method AFAIK. If you override any
method with the same signature, you must call super to invoke the
previous version (up the chain).

BTW, Ruby for Rails by David Black has a very clear description of
method lookup that I just happened to be reading this morning. So its
his fault if I'm wrong about this :-)
Keith
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-05-10 19:38
Upon further reflection, my entire problem boiled down to not
understanding the "super" keyword in Ruby, which means "execute the same
method in my superclass", not "the superclass object" (which is what it
means in Java).

Which, practically speaking, is more what you want anyway (I can't think
of a reason that you would ever want to sling around an instance of a
superclass, but I'll have to consider that).

Thanks,
Wes

Keith Lancaster wrote:
> Wes Gamble wrote:
>> All,
>>
>> I just seem to have had a revelation about how the super keyword in Ruby
>> is ever so slightly different from the super keyword in Java.
>>
>> In general, given
>>
>>   class B < A
>>
>> and desiring to add an initialize method to B, is it a "best practice"
>> to always do
>>
>>   class B
>>   ...
>>   def initialize
>>     super
>>     ... custom B initialization here ...
>>   end
>>
>> Basically, I'm trying to understand that if I instantiate B, then
>> A.initialize doesn't get called by default - is that correct?  Is that
>> so that B can completely override the initialization details of itself?
>>
>> Thanks,
>> Wes
>
> A.intialize does not get called by default - you do have to call super.
> This is not unique to the initialize method AFAIK. If you override any
> method with the same signature, you must call super to invoke the
> previous version (up the chain).
>
> BTW, Ruby for Rails by David Black has a very clear description of
> method lookup that I just happened to be reading this morning. So its
> his fault if I'm wrong about this :-)
> Keith
This topic is locked and can not be replied to.