[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

calling an arbitrary superclass method

Eric Mahurin

7/3/2005 7:31:00 PM

Anybody know of a way to call an arbitrary method in the
superclass? In the current method, you can use "super" to call
the same method of the superclass, but how do you call another?
The only way I can think of off hand is to alias the
superclass methods to something else before creating the
derived class definition. But, that seems kind of ugly. It
seems like there should be a way to call it directly using
superclass or something.





__________________________________
Yahoo! Mail Mobile
Take Yahoo! Mail with you! Check email on your mobile phone.
http://mobile.yahoo.com/...


6 Answers

Logan Capaldo

7/3/2005 8:32:00 PM

0


On Jul 3, 2005, at 3:31 PM, Eric Mahurin wrote:

> Anybody know of a way to call an arbitrary method in the
> superclass? In the current method, you can use "super" to call
> the same method of the superclass, but how do you call another?
> The only way I can think of off hand is to alias the
> superclass methods to something else before creating the
> derived class definition. But, that seems kind of ugly. It
> seems like there should be a way to call it directly using
> superclass or something.
>
>
>
>
>
> __________________________________
> Yahoo! Mail Mobile
> Take Yahoo! Mail with you! Check email on your mobile phone.
> http://mobile.yahoo.com/...
>
>


self.class.superclass.instance_method(:some_method).bind(self).call
(*args)




Eric Mahurin

7/3/2005 9:03:00 PM

0

--- Logan Capaldo <logancapaldo@gmail.com> wrote:
> On Jul 3, 2005, at 3:31 PM, Eric Mahurin wrote:
>
> > Anybody know of a way to call an arbitrary method in the
> > superclass? In the current method, you can use "super" to
> call
> > the same method of the superclass, but how do you call
> another?
> > The only way I can think of off hand is to alias the
> > superclass methods to something else before creating the
> > derived class definition. But, that seems kind of ugly.
> It
> > seems like there should be a way to call it directly using
> > superclass or something.

> self.class.superclass.
> instance_method(:some_method).bind(self).call(*args)

That is the magic I was looking for. Thanks.

I think it would be nice if we had something like this in
Object to make it easier (and faster when written in C):

class Object
def super_method(sym)
self.class.superclass.instance_method(sym)
end
def super_send(sym,*args)
super_method(sym).call(*args)
end
end





____________________________________________________
Yahoo! Sports
Rekindle the Rivalries. Sign up for Fantasy Football
http://football.fantasysports...


Eric Mahurin

7/3/2005 9:06:00 PM

0

--- Eric Mahurin <eric_mahurin@yahoo.com> wrote:

> --- Logan Capaldo <logancapaldo@gmail.com> wrote:
> > On Jul 3, 2005, at 3:31 PM, Eric Mahurin wrote:
> >
> > > Anybody know of a way to call an arbitrary method in the
> > > superclass? In the current method, you can use "super"
> to
> > call
> > > the same method of the superclass, but how do you call
> > another?
> > > The only way I can think of off hand is to alias the
> > > superclass methods to something else before creating the
> > > derived class definition. But, that seems kind of ugly.
> > It
> > > seems like there should be a way to call it directly
> using
> > > superclass or something.
>
> > self.class.superclass.
> > instance_method(:some_method).bind(self).call(*args)
>
> That is the magic I was looking for. Thanks.
>
> I think it would be nice if we had something like this in
> Object to make it easier (and faster when written in C):
>
> class Object
> def super_method(sym)
> self.class.superclass.instance_method(sym).bind(self)
> end
> def super_send(sym,*args)
> super_method(sym).call(*args)
> end
> end

Forgot the bind(self). Added it above.


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail...


redentis

7/4/2005 12:11:00 PM

0

Eric Mahurin wrote:
> --- Logan Capaldo <logancapaldo@gmail.com> wrote:
> > On Jul 3, 2005, at 3:31 PM, Eric Mahurin wrote:
> >
> > > Anybody know of a way to call an arbitrary method in the
> > > superclass? In the current method, you can use "super" to
> > call
> > > the same method of the superclass, but how do you call
> > another?
> > > The only way I can think of off hand is to alias the
> > > superclass methods to something else before creating the
> > > derived class definition. But, that seems kind of ugly.
> > It
> > > seems like there should be a way to call it directly using
> > > superclass or something.
>
> > self.class.superclass.
> > instance_method(:some_method).bind(self).call(*args)
>
> That is the magic I was looking for. Thanks.
>
> I think it would be nice if we had something like this in
> Object to make it easier (and faster when written in C):
>
> class Object
> def super_method(sym)
> self.class.superclass.instance_method(sym)
> end
> def super_send(sym,*args)
> super_method(sym).call(*args)
> end
> end

What scenario(s) do you think this would be useful in? Surely the whole
point of the OO principles of polymorphism and implementation hiding is
that you should never need to "call an arbitrary method in the
superclass".

The only case I can think of where you might want to access the
superclass implementation is in an overriding method in a subclass and
at that point 'super' does what you'd expect: gives you essentially
privileged access to the implementation in the super class.

Eric Mahurin

7/4/2005 12:37:00 PM

0

--- redentis <redentis@gmail.com> wrote:

> Eric Mahurin wrote:
> > --- Logan Capaldo <logancapaldo@gmail.com> wrote:
> > > On Jul 3, 2005, at 3:31 PM, Eric Mahurin wrote:
> > >
> > > > Anybody know of a way to call an arbitrary method in
> the
> > > > superclass? In the current method, you can use "super"
> to
> > > call
> > > > the same method of the superclass, but how do you call
> > > another?
> > > > The only way I can think of off hand is to alias the
> > > > superclass methods to something else before creating
> the
> > > > derived class definition. But, that seems kind of
> ugly.
> > > It
> > > > seems like there should be a way to call it directly
> using
> > > > superclass or something.
> >
> > > self.class.superclass.
> > > instance_method(:some_method).bind(self).call(*args)
> >
> > That is the magic I was looking for. Thanks.
> >
> > I think it would be nice if we had something like this in
> > Object to make it easier (and faster when written in C):
> >
> > class Object
> > def super_method(sym)
> > self.class.superclass.instance_method(sym).bind(self)
> > end
> > def super_send(sym,*args)
> > super_method(sym).call(*args)
> > end
> > end
>
> What scenario(s) do you think this would be useful in? Surely
> the whole
> point of the OO principles of polymorphism and implementation
> hiding is
> that you should never need to "call an arbitrary method in
> the
> superclass".
>
> The only case I can think of where you might want to access
> the
> superclass implementation is in an overriding method in a
> subclass and
> at that point 'super' does what you'd expect: gives you
> essentially
> privileged access to the implementation in the super class.

That is exactly what I want to do. I want to be able to call
more than just the superclass method of the same name that
"super" allows me to.

These methods should be "protected" so that only derived
classes are using them.




____________________________________________________
Yahoo! Sports
Rekindle the Rivalries. Sign up for Fantasy Football
http://football.fantasysports...


Robert Klemme

7/4/2005 1:38:00 PM

0

Eric Mahurin wrote:
> --- redentis <redentis@gmail.com> wrote:
>
>> Eric Mahurin wrote:
>>> --- Logan Capaldo <logancapaldo@gmail.com> wrote:
>>>> On Jul 3, 2005, at 3:31 PM, Eric Mahurin wrote:
>>>>
>>>>> Anybody know of a way to call an arbitrary method in the
>>>>> superclass? In the current method, you can use "super" to call
>>>>> the same method of the superclass, but how do you call another?
>>>>> The only way I can think of off hand is to alias the
>>>>> superclass methods to something else before creating the
>>>>> derived class definition. But, that seems kind of ugly. It
>>>>> seems like there should be a way to call it directly using
>>>>> superclass or something.
>>>
>>>> self.class.superclass.
>>>> instance_method(:some_method).bind(self).call(*args)
>>>
>>> That is the magic I was looking for. Thanks.
>>>
>>> I think it would be nice if we had something like this in
>>> Object to make it easier (and faster when written in C):
>>>
>>> class Object
>>> def super_method(sym)
>>> self.class.superclass.instance_method(sym).bind(self)
>>> end
>>> def super_send(sym,*args)
>>> super_method(sym).call(*args)
>>> end
>>> end
>>
>> What scenario(s) do you think this would be useful in? Surely
>> the whole
>> point of the OO principles of polymorphism and implementation
>> hiding is
>> that you should never need to "call an arbitrary method in
>> the
>> superclass".
>>
>> The only case I can think of where you might want to access
>> the
>> superclass implementation is in an overriding method in a
>> subclass and
>> at that point 'super' does what you'd expect: gives you
>> essentially
>> privileged access to the implementation in the super class.
>
> That is exactly what I want to do. I want to be able to call
> more than just the superclass method of the same name that
> "super" allows me to.
>
> These methods should be "protected" so that only derived
> classes are using them.

But then what do you need the explicitely selection of the class for?
Just call the method - if it's overridden then that version is invoked
(and can use super to invoke the super class impl); if not the super class
method is invoked anyway.

If you need to selectively call methods of specific super classes that are
overridden that's a strong indication that a refactoring is in order IMHO.

Kind regards

robert