In Ruby, is there a way to \'override\' a constant in a subclass in such a way that calling an inherited method from the subclass results in that method using the new consta
If you have the luxury of being able to change the base class, consider wrapping the "constants" that need changing in class methods in the base class and overriding them as needed in subclasses. This removes the potential for confusion between parent and subclass constants. For the example, this would be as follows:
class SuperClass
CONST = "Hello, world!".freeze
def self.const
CONST
end
def self.say_hello
const
end
end
class SubClass < SuperClass
CONST = "Hello, Bob!".freeze
def self.const
CONST
end
end
SubClass.say_hello #=> "Hello, Bob!