From: Shiwei Zhang Date: 2006-12-08T17:12:27+09:00 Subject: Re: Calling super in overwritten methods --------------010200060300050701010805 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit self.class.superclass.instance_method( :knox ).bind( self ).call. It looks tricky and even clunky. Actually this function can be easily realized by C++' compulsory type conversion, as follows: #include "stdio.h" class A { public: int method1(); int method2(); }; class B: public A { public: int method1(); int method2(); }; int static main() { B *obj=new B(); printf("%d\n",((A)*obj).method1()); return 0; } int A::method1() { return method2();} int A::method2() { return 100;} int B::method1() { return method2();} int B::method2() { return 200;} However regarding ruby, as in joergd@pobox.com's program, the derived object always invokes the method in its own context, i.e., it invokes method2 of the B object rather than the A object. Anyhow, ruby provides Class Method (different with the method tied to objects) for us, so if you intend to invoke the method of the parent from the derived class which has the same method name, you may define and use Class Method. E.g., def A.method2 return 100; end Shiwei The views expressed are my own and not necessarily those of Oracle and its affiliates. hemant wrote: > On 12/6/06, 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? >> >> 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 > > --------------010200060300050701010805--