Ruby constant lookup
When you reference a constant in ruby code, how is the right definition found?
First ruby checks the lexical scope of where the constant was referenced. That is the enclosing class or module. If no match is found ruby will continue to look for the reference in the next enclosing modules and classes.
Module.nesting can be used to investigate which modules and classes are taken into account.
If the definition is not found in the lexically enclosing scope, ruby will look into the inheritance hierarchy of the module or class that referenced the constant.
Constants that are defined on Object class are so called “top-level” constants. It is worthwile noting that since ruby class inherit from Object, they eventually have access to the constants defined by it.
If the definition still hasn’t been found ruby will envoke the
const_missing method of the class or module referencing to the constant.