Suppose you have the following Ruby classes:
class A
def self.greet
puts 'hi'
end
end
class B < A; end;
class C < B; end;
Is it possible to redefine greet on B in such a way that C still uses the original definition without modifying C?
(I understand that “you’re doing it wrong” is a probable reaction, but please assume for a moment that there’s a good reason for doing this.)
Update
It’s good to see yet another demonstration that Ruby will let you do whatever you want. However, to anyone finding this question and thinking of doing likewise, I should say that I did eventually realize I was doing it wrong; C needed to subclass A directly.
Read on to receive your shotgun, and watch where you’re aiming it. 🙂
You could do something like:
This means that B is aware that you want to change its version of
greetbut only forBand no other decedent classes.