How do I call a grand-parent's method, and skipping the parent in ruby
How do I choose a particular a method call in the inheritance chain?
class A
def boo; puts "A:Boo"; end
end
class B < A
def boo; super; puts "B:Boo"; end
end
class C < B
de开发者_如何学Gof boo; self.A.boo(???); puts "C:Boo"; end
end
Thus the output would be A:Boo, C:Boo
TIA,
-daniel
You can do
class C < B
def boo
A.instance_method(:boo).bind(self).call
puts "C:Boo"
end
end
However if you need this, that's usually an indicator that you should rethink your design. In particular if C needs A's implementation of boo
, maybe B should not override it.
精彩评论