31 Aug '17, 1pm

TIL: "Warning: Toplevel constant X referenced by Y"

To achieve this, Rails overwrites Module#const_missing to know when a new class might need to be loaded. It then guesses the filename from the constant name, loads the class and passes on execution as if nothing ever happened. Rails autoloading relies on #const_missing being triggered and in our case this does not happen because Ruby has found the toplevel constant instead. In our case the correct class is never loaded and execution continues with a wrong class. Even worse, whether or not it fails can depend on the order in which the classes are auto-loaded, adding a non-deterministic component to make debugging extra fun.

Full article: http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-cons...

Tweets