Newbie question about initialize methods in subclasses


#1

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


#2

Wes G. 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 :slight_smile:
Keith


#3

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 L. wrote:

Wes G. 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 :slight_smile:
Keith