Can super call a method multiple classes higher in the hierarchy?


(Ian Macdonald) #1

Hi,

Am I right in thinking that if I call super from method foo in class
Bar, it does not matter that no identically named method is defined in
Bar’s parent class, just as long as an identically named method is
defined in the grandparent or an even further removed ancestor class
of Bar?

It seems to work that way, but I don’t remember reading that
explicitly in the pickaxe book.

Ian

···


Ian Macdonald | “The four building blocks of the universe
ian@caliban.org | are fire, water, gravel and vinyl.” –
> Dave Barry
>
>


(Yohanes Santoso) #2

Ian Macdonald ian@caliban.org writes:

Hi,

Am I right in thinking that if I call super from method foo in class
Bar, it does not matter that no identically named method is defined in
Bar’s parent class, just as long as an identically named method is
defined in the grandparent or an even further removed ancestor class
of Bar?

That’s what inheritance in OOP does.

irb(main):001:0> class A
irb(main):002:1> def hallo
irb(main):003:2> puts "HALLO FROM A"
irb(main):004:2> end
irb(main):005:1> end
nil
irb(main):006:0> class B < A
irb(main):007:1> end
nil
irb(main):008:0> class C < B
irb(main):009:1> def hallo
irb(main):010:2> super
irb(main):011:2> puts "HALLO FROM C"
irb(main):012:2> end
irb(main):013:1> end
nil
irb(main):014:0> C.new.hallo
HALLO FROM A
HALLO FROM C
nil

YS.


(Juergen Katins) #3

“Ian Macdonald” ian@caliban.org schrieb :

Am I right in thinking that if I call super from method foo in class
Bar, it does not matter that no identically named method is defined in
Bar’s parent class, just as long as an identically named method is
defined in the grandparent or an even further removed ancestor class
of Bar?

It seems to work that way, but I don’t remember reading that
explicitly in the pickaxe book.

Look at the paragraph atop of this link:
http://home.vr-web.de/juergen.katins/ruby/book/tut_classes.html#Message!send
ing saying:
"The answer has to do with the way Ruby determines which method should be
called when …"
A similar problem is described there.

Juergen Katins


(Juergen Katins) #4

Sorry, link must be

http://home.vr-web.de/juergen.katins/ruby/book/tut_classes.html#Message!send
ing

Juergen Katins


(Ian Macdonald) #5

Thanks. I’d already read that section in the pickaxe book and, while
it came close to what I was looking for, it didn’t convince me that
super wasn’t a special case.

Ian

···

On Thu 20 Jun 2002 at 16:47:46 +0900, Juergen Katins wrote:

“Ian Macdonald” ian@caliban.org schrieb :

Am I right in thinking that if I call super from method foo in class
Bar, it does not matter that no identically named method is defined in
Bar’s parent class, just as long as an identically named method is
defined in the grandparent or an even further removed ancestor class
of Bar?

It seems to work that way, but I don’t remember reading that
explicitly in the pickaxe book.

Look at the paragraph atop of this link:
http://home.vr-web.de/juergen.katins/ruby/book/tut_classes.html#Message!send
ing saying:
"The answer has to do with the way Ruby determines which method should be
called when …"
A similar problem is described there.


Ian Macdonald | You’ll be a guest at a gay party that will
ian@caliban.org | have important consequences for you.
>
>
>