Ruby: Is it possible to redefine a method but leave subclasses unchanged?

ruby

Solution

You could do something like:

def B.greet
  puts self == B ? 'beeeee' : super
end

This means that B is aware that you want to change its version of `greet` but only for `B` and no other decedent classes.

Problem

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. :)

Original source