Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling super's super method

Tags:

ruby

Is is possible to do something like super.super in the overriden method? That is, to bypass the direct parent's super and call "grandparent's" super?

like image 990
Vincent Avatar asked Dec 29 '10 19:12

Vincent


1 Answers

This is not recommended, but what you want is possible like this:

grandparent = self.class.superclass.superclass
meth = grandparent.instance_method(:the_method)
meth.bind(self).call

This works by first getting the grandparent class, then calling instance_method on it to get an UnboundMethod representing the grandparent's version of the_method. It then uses UnboundMethod#bind and Method#call to call the grandparent's method on the current object.

like image 133
sepp2k Avatar answered Oct 07 '22 18:10

sepp2k