Newbie question about initialize methods in subclasses

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

···

--
Posted via http://www.ruby-forum.com/.

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 :slight_smile:
Keith

···

--
Posted via http://www.ruby-forum.com/\.

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 :slight_smile:
Keith

--
Posted via http://www.ruby-forum.com/\.