Calling super's super method

前端 未结 3 2103
慢半拍i
慢半拍i 2021-02-07 10:11

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?

3条回答
  •  情歌与酒
    2021-02-07 10:57

    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.

提交回复
热议问题