Let’s say you have a class Foo:
|
class Foo THE_CONST = 'parent_const' def get_val 'parent_val' end def show_const puts THE_CONST end def show_val puts get_val end end |
Further, let’s say you have a class Bar, derived from Foo, that redefines a couple of things:
|
class Bar < Foo THE_CONST = 'child_const' def get_val 'child_val' end end |
It seems kind of obvious that:
|
f = Foo.new f.show_const # => parent_const f.show_val # => parent_val |
But would you have guessed that:
|
b = Bar.new b.show_const # => parent_const WTF!?!?! b.show_val # => child_val |
This caused me no end of headaches during a weekend bout of refactoring. Pushing default constants up to the parent class completely hosed me up in those child classes that tried to override them. Overriding dumb getter methods that just return constants, though, works fine.
I’m sure there’s some computer-sciencey reason for this that Matz could explain to me, but I had really expected the every-call-is-a-message nature of Ruby to handle those two cases more consistently.