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 constant instead of the old one? For example:
class SuperClass
CONST = "Hello, world!"
def self.say_hello
CONST
end
end
class SubClass < SuperClass
override_const :CONST, "Hello, Bob!"
end
SuperClass.say_hello # => "Hello, world!"
SubClass.say_hello # => "Hello, Bob!"
If not, is there perhaps a way to do something like this instead?
class SuperClass
CONST = "Hello, world!"
def self.say_hello
CONST
end
end
SubClass = SuperClass.clone
SubClass.send(:remove_const, :CONST)
SubClass.const_set(:CONST, "Hello, Bob!")
SubClass.say_hello # => "Hello, Bob!"
Note: I tried my second example out in irb, and it seems to work except that class methods can't seem to access CONST after I clone the class:
irb(main):012:0> SubClass.say_hello
NameError: uninitialized constant Class::CONST
from (irb):4:in `say_hello'
from (irb):12
from C:/Ruby193/bin/irb:12:in `<main>'