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?

- 16,086
- 18
- 67
- 73
-
...I can't help but think this question is, in fact, a song by [They Might Be Giants](http://en.wikipedia.org/wiki/They_Might_Be_Giants)... O.o ...sorry. – David Thomas Dec 29 '10 at 20:34
-
8So basically you want a 'superduper' method? :) – Stephen Petschulat Dec 29 '10 at 22:36
3 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.

- 363,768
- 54
- 674
- 675
you could modify the method's arguments to allow some kind of optional 'pass to parent' parameter. in the super class of your child, check for this parameter and if so, call super from that method and return, otherwise allow execution to continue.
class Grandparent; def method_name(opts={}); puts "Grandparent called."; end; end
class Parent < Grandparent
def method_name(opts={})
return super if opts[:grandparent]
# do stuff otherwise...
puts "Parent called."
end
end
class Child < Parent
def method_name(opts={})
super(:grandparent=>true)
end
end
ruby-1.9.2-p0 > Child.new.method_name
Grandparent called.
=> nil
otherwise i agree with @Femaref, just b/c something is possible doesn't mean it's a good idea. reconsider your design if you think this is necessary.

- 659
- 6
- 9
Considering this is breaking one of the principles of OOP (encapsulation), I dearly hope it isn't possible. Even the case of you trying to do this speaks of a problem with your design.

- 60,705
- 7
- 138
- 176
-
1
-
2I'm sorry, but have you heard about inheritance diamonds? This is how they can be resolved in Ruby. So let's not make inheritance diamonds, or let's change Ruby to accomodate them, you might say. I will not argue. What you say might hold in the ideal world. But in the real world, you are working with the code written by bad programmers, which you have to wire together without having the right to modify it. And this is what these screwdrivers are for. – Boris Stitnicky Aug 18 '13 at 04:37