Constant Lookup

When a constant is referenced without any qualifying namespace, the Ruby interpreter must find the appropriate definition of the constant. To do so, it uses a name resolution algorithm, just as it does to find method definitions. However, constants are resolved much differently than methods.

Ruby first attempts to resolve a constant reference in the lexical scope of the reference. This means that it first checks the class or module that encloses the constant reference to see if that class or module defines the constant. If not, it checks the next enclosing class or module. This continues until there are no more enclosing classes or modules. Note that top-level or “global” constants are not considered part of the lexical scope and are not considered during this part of constant lookup. The class method Module.nesting returns the list of classes and modules that are searched in this step, in the order they are searched.

If no constant definition is found in the lexically enclosing scope, Ruby next tries to resolve the constant in the inheritance hierarchy by checking the ancestors of the class or module that referred to the constant. The ancestors method of the containing class or module returns the list of classes and modules searched in this step.

If no constant definition is found in the inheritance hierarchy, then top-level constant definitions are checked.

If no definition can be found for the desired constant, then the const_missing method—if there is one—of the containing class or module is called and given the opportunity to provide a value for the constant. This const_missing hook is covered in Chapter 8, and Example 8-3 illustrates its use.

There are a few points about this constant lookup algorithm that are worth noting in more detail:

  • Constants defined in enclosing modules are found in preference to constants defined in included modules.

  • The modules included by a class are searched before the superclass of the class.

  • The Object class is part of the inheritance hierarchy of all classes. Top-level constants, defined outside of any class or module, are like top-level methods: they are implicitly defined in Object. When a top-level constant is referenced from within a class, therefore, it is resolved during the search of the inheritance hierarchy. If the constant is referenced within a module definition, however, an explicit check of Object is needed after searching the ancestors of the module.

  • The Kernel module is an ancestor of Object. This means that constants defined in Kernel behave like top-level constants but can be overridden by true top-level constants, that are defined in Object.

Example 7-1 defines and resolves constants in six different scopes and demonstrates the constant name lookup algorithm described previously.

Example 7-1. Constant name resolution

module Kernel
  # Constants defined in Kernel
  A = B = C = D = E = F = "defined in kernel"
end

# Top-level or "global" constants defined in Object
A = B = C = D = E = "defined at toplevel"

class Super
  # Constants defined in a superclass
  A = B = C = D = "defined in superclass"
end

module Included
  # Constants defined in an included module
  A = B = C = "defined in included module"
end

module Enclosing
  # Constants defined in an enclosing module
  A = B = "defined in enclosing module"

  class Local < Super
    include Included

    # Locally defined constant
    A = "defined locally"

    # The list of modules searched, in the order searched
    # [Enclosing::Local, Enclosing, Included, Super, Object, Kernel]
    search = (Module.nesting + self.ancestors + Object.ancestors).uniq

    puts A  # Prints "defined locally"
    puts B  # Prints "defined in enclosing module"
    puts C  # Prints "defined in included module"
    puts D  # Prints "defined in superclass"
    puts E  # Prints "defined at toplevel"
    puts F  # Prints "defined in kernel"
  end
end
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.79.33