Sorry if I did change the thread, but I seem to have at least clarified the
question :))
(thanks to everybody for helped so far)
After some research, I still seem to be unable to find the answer to my
question…
So, how do I call a method of my superclass when I have a method with the
same name in my current class (i.e. is there any other way beside aliasing)
?
class A
def foo
puts "A#foo"
end
def bar
puts "A#bar"
end
end
class B
def foo
super #okay, calls A#foo
puts "B#foo"
end
def bar
super.foo ?? how do I do this
end
end
class B < A
alias :aFoo :foo # refer to A::foo
def foo
puts “B::foo”
aFoo
end
end
···
On Tuesday 04 June 2002 02:37 pm, Philip Mateescu wrote:
So, how do I call a method of my superclass when I have a method
with the same name in my current class (i.e. is there any other way
beside aliasing) ?
I ran into this before as well. I was very surprised to find
that #caller returned an Array of Strings (!!!). Very disappointed,
I was really hoping it returned an Array of Objects themselves.
I ran into this same problem with Java and was disappointed (you
know, not being able to get a stack trace except as a string-based
value).
I was hoping to do something like:
class A
def foo
puts “A#foo”
end
def bar
puts “A#bar”
caller[0].baz
end
def baz
puts “A#baz”
end
end
a = A.new
p a.foo # => “A#foo”
p a.baz # => “A#baz”
p a.bar # => “A#bar”\n"A#baz"
I don’t know how useful this would be (or if it’s possible to
cleanly implement) but it’s something I tried to make work
at one point and gave up …
So, how do I call a method of my superclass when I have a method with the
same name in my current class (i.e. is there any other way beside aliasing)
[…]
def bar
super.foo ?? how do I do this
end
–
Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)
From a personal point of view, aliasing, while I see it’s a great technique,
it somehow seem to me it dimishes the dynamicality of code. I mean you have
to alias all the methods you wanna use from the superclass (and which are
overridden in the current class)…
thanks a lot,
philip
···
----- Original Message -----
From: “Ned Konz” ned@bike-nomad.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, 05 June, 2002 12:59 AM
Subject: Re: Calling a (any) superclass method when already have one by that
name
On Tuesday 04 June 2002 02:37 pm, Philip Mateescu wrote:
So, how do I call a method of my superclass when I have a method
with the same name in my current class (i.e. is there any other way
beside aliasing) ?
What’s wrong with aliasing?
class A
def foo
puts “A::foo”
end
end
class B < A
alias :aFoo :foo # refer to A::foo
def foo
puts “B::foo”
aFoo
end
end
I ran into this before as well. I was very surprised to find
that #caller returned an Array of Strings (!!!). Very disappointed,
I was really hoping it returned an Array of Objects themselves.
I ran into this same problem with Java and was disappointed (you
know, not being able to get a stack trace except as a string-based
value).
But how often does that happen? In my experience with Smalltalk, not
too often (though it’s easy to call a superclass method in
Smalltalk).
Given a properly factored design, it’s pretty uncommon to need to call different superclass methods directly (the common case of calling
the same superclass method is already handled with super()).
I would argue that if you find yourself having to do this, you
probably need to refactor the code and extract the bit that is needed
by both the superclass and subclass into a separate superclass
method.
If your subclass doesn’t change the meaning of a method, then you
should be calling your overridden method. Which is easy.
···
On Tuesday 04 June 2002 03:40 pm, Philip Mateescu wrote:
Wrong ? Nothing, I just wanted another way…
it somehow seem to me it dimishes the dynamicality of code. I mean
you have to alias all the methods you wanna use from the superclass
(and which are overridden in the current class)…
From a personal point of view, aliasing, while I see it’s a great technique,
it somehow seem to me it dimishes the dynamicality of code. I mean you have
to alias all the methods you wanna use from the superclass (and which are
overridden in the current class)…
To me, the bad smell is that you’re trying to call the superclass
implementation of a method in your current object. Why is it that
you’re trying to do this?
This also reduces the dynamism of your code, while torturing the
mind.