views:

58

answers:

1

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
  def boo; self.A.boo(???); puts "C:Boo"; end
end

Thus the output would be A:Boo, C:Boo

TIA,

-daniel

+4  A: 

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.

sepp2k
I would put it the other way around: if a C should not behave like a B, then C shouldn't be a subclass of B.
Jörg W Mittag
@JörgWMittag: Good point.
sepp2k
The class structure is A->B->C, but I wanted to avoid doing what B did and I overloaded, but I still needed to honor part of the API that A was providing which was a part of an plugin architecture. Class B has 99% of the stuff I wanted to do though!
Daniel