From: Andrew Johnson Date: 2004-10-01T05:20:01+09:00 Subject: Re: Calling super methods On Thu, 30 Sep 2004 13:02:58 -0400, Grzegorz Dostatni wrote: > > Is it possible to call a method of a superclass? Let's say I have a > instance of class Bar ( subclassed from Foo). > Both Foo and Bar define a function h() > > I want to call a.h() and have it go the Foo's implementation if it, > without going through the super() call (ie. do it from outside the > object). > > I have a code in python that does it: > > class Foo: > def h(self): > print "Foo" > > class Bar(Foo): > def h(self): > print "Bar" > > b = Bar() > > b.h() >>> prints out "Bar" > Foo.h(b) >>> prints out "Foo" Perhaps there's a shorter call sequence, but ... class Foo def h; puts "foo";end end class Bar < Foo def h; puts "bar";end end a = Bar.new a.h #=> 'bar' ## hardcode the call-up class: Foo.instance_method(:h).bind(a).call #=> 'foo' ## more dynamic call-up: a.class.superclass.instance_method(:h).bind(a).call #=> 'foo' ## Or, add dynamic call_up to the Object class: class Object def supercede(meth) self.class.superclass.instance_method(meth).bind(self).call end alias :call_up :supercede end a.h #=> 'bar' a.supercede(:h) #=> 'foo' a.call_up(:h) #=> 'foo' __END__ -- Andrew L. Johnson http://www.siaris.net/ They're not soaking, they're rusting! -- my wife (on my dishwashing habits)