From: Florian Gross Date: 2004-10-01T08:15:00+09:00 Subject: Re: Calling super methods --------------010802000800080906080708 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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). Ah, this has been asked before. I attached my implementation. With this one you don't need to pass the method name. Regards, Florian Gross --------------010802000800080906080708 Content-Type: text/plain; name="superjump.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="superjump.rb" # Like super this calls methods from the inheritance chain which this # method is replacing. However this version jumps over one or more # methods in the inheritance chain. It is used like this: # # class X # def it; p "X#it"; end # end # # class Y < X # def it; p "Y#it"; end # end # # class Z < Y # def it # p "Z#it" # superjump # end # end # # Z.new.it # outputs "X#it", "Z#it" # # Be careful. This method can't automatically pass the arguments of the # caller like super does. You'll have to manually supply them. def superjump(gap = 0, *args, &block) method_name = caller[0][/in `(.*?)'/, 1].intern klass = self.class.ancestors[1 + gap] klass.instance_method(method_name).bind(self).call(*args, &block) end --------------010802000800080906080708--