[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Calling super in overwritten methods

Joerg Diekmann

12/6/2006 10:36:00 AM

Hi - not sure if this is possible - but it feels like it could be with
some serious ruby-fu.

I have the following:

class A
def method1
method2
end

def method2
return 100
end
end

class B < A
def method1
super
end

def method2
return 200
end
end


This is what happens:

b.method1 # 200

But, what I want is:

b.method1 # 100


Is this possible at all?

Thanks
Joerg

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

10 Answers

Paul Lutus

12/6/2006 10:45:00 AM

0

Joerg Diekmann wrote:

> Hi - not sure if this is possible - but it feels like it could be with
> some serious ruby-fu.
>
> I have the following:
>
> class A
> def method1
> method2
> end
>
> def method2
> return 100
> end
> end
>
> class B < A
> def method1
> super
> end
>
> def method2
> return 200
> end
> end
>
>
> This is what happens:
>
> b.method1 # 200
>
> But, what I want is:
>
> b.method1 # 100
>
>
> Is this possible at all?

No, not without some tortured coding. You have redefined method2 in your
derived class, consequently this redefinition takes precedence in instances
of the derived class, and this is by design, not by accident.

Remember that the call to "method2" in the parent class, called with "super"
as you show it, is still in the context of an instance of the derived
class, so the call is resolved to refer to the derived class method2, not
the parent one.

If, as you say, you really want the result of the parent's method2, then
don't redefine that method in the derived class. Or use different method
names to create the distinction you want.

--
Paul Lutus
http://www.ara...

Joerg Diekmann

12/6/2006 10:54:00 AM

0

Ok pity there's no simple Ruby solution to it. I'll just have to rename
my methods in my derived class then. Thanks though!


> No, not without some tortured coding. You have redefined method2 in your
> derived class, consequently this redefinition takes precedence in
> instances
> of the derived class, and this is by design, not by accident.
>

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

hemant

12/6/2006 11:58:00 AM

0

On 12/6/06, Joerg Diekmann <joergd@pobox.com> wrote:
> Hi - not sure if this is possible - but it feels like it could be with
> some serious ruby-fu.
>
> I have the following:
>
> class A
> def method1
> method2
> end
>
> def method2
> return 100
> end
> end
>
> class B < A
> def method1
> super
> end
>
> def method2
> return 200
> end
> end
>
>
> This is what happens:
>
> b.method1 # 200
>
> But, what I want is:
>
> b.method1 # 100
>
>
> Is this possible at all?
>
> Thanks
> Joerg

Indeed but sometime back, we had similar discussion here and someone
(David I guess) proposed:

class Parent
def knox
puts 'parent'
end
end

class Child < Parent
def knox
puts 'child'
end
def test
self.class.superclass.instance_method( :knox ).bind( self ).call
end
end

Child.new.test


--
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.

paul

12/6/2006 12:21:00 PM

0

Ok, that qualifies as 'ruby-fu' :)

> self.class.superclass.instance_method( :knox ).bind( self ).call

Joerg Diekmann

12/6/2006 12:55:00 PM

0

Hmmm ... that doesn't work for me.


class A
def method1
method2
end

def method2
return 100
end
end

class B < A
def method1
self.class.superclass.instance_method( :method1 ).bind( self ).call
end

def method2
return 200
end
end

b.method1 # Still 200 instead of 100



Hemant Kumar wrote:
> Indeed but sometime back, we had similar discussion here and someone
> (David I guess) proposed:
>
> class Parent
> def knox
> puts 'parent'
> end
> end
>
> class Child < Parent
> def knox
> puts 'child'
> end
> def test
> self.class.superclass.instance_method( :knox ).bind( self ).call
> end
> end
>
> Child.new.test


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

Trans

12/6/2006 4:32:00 PM

0


Joerg Diekmann wrote:
> Hi - not sure if this is possible - but it feels like it could be with
> some serious ruby-fu.
>
> I have the following:
>
> class A
> def method1
> method2
> end
>
> def method2
> return 100
> end
> end
>
> class B < A
> def method1
> super
> end
>
> def method2
> return 200
> end
> end
>
>
> This is what happens:
>
> b.method1 # 200
>
> But, what I want is:
>
> b.method1 # 100
>
>
> Is this possible at all?

Local methods would do the trick. We don't have those. But here's a
cool way:

require 'facets'
require 'kernel/as'

class A

def method1
as(A).method2
end

def method2
return 100
end

end

class B < A
def method1
super
end

def method2
return 200
end
end

T.

(http://facets.rub...)


Ara.T.Howard

12/6/2006 5:00:00 PM

0

ThoML

12/7/2006 8:48:00 AM

0

You didn't say if you can change class a if so you should probably
start the redesign there before resorting to vodoo.

= class A
= def method1
* method2_A
= end
=
= def method2
= return 100
= end
+ alias :method2_A :method2
= end
=
= class B < A
= def method1
= super
= end
=
= def method2
= return 200
= end
+ alias :method2_B :method2
= end

or so. This makes your intentions quite clear I'd say.

Brian

12/8/2006 11:24:00 AM

0

Hey,
Assuming your trying to make a determination in class B as to which
method to use, you could make method2 a class method.

class A
def method1
A.method2
end

def self.method2
return 100
end
end

class B < A
def method1
x = 1
puts A.method2 if x == 1 # => 100
puts method2 if x == 2 # => 200
end

def method2
return 200
end
end

B.new.method1

- Brian

On Dec 6, 5:36 am, Joerg Diekmann <joe...@pobox.com> wrote:
> Hi - not sure if this is possible - but it feels like it could be with
> some serious ruby-fu.
>
> I have the following:
>
> class A
> def method1
> method2
> end
>
> def method2
> return 100
> end
> end
>
> class B < A
> def method1
> super
> end
>
> def method2
> return 200
> end
> end
>
> This is what happens:
>
> b.method1 # 200
>
> But, what I want is:
>
> b.method1 # 100
>
> Is this possible at all?
>
> Thanks
> Joerg
>
> --
> Posted viahttp://www.ruby-....


David Vallner

12/8/2006 10:28:00 PM

0

Joerg Diekmann wrote:
> Ok pity there's no simple Ruby solution to it. I'll just have to rename
> my methods in my derived class then. Thanks though!
>
>

Having a "simple" way would be way too C++, since it -is- horribly
breaking object-oriented behaviour. Might as well bring back "virtual".

The unbound method Ruby fu is just right in my opinion, it makes it
instantly clear that the code isn't behaving polymorphically, but uses
the class as a function namespace.

David Vallner