const_missing
.nesting
retorna la lista de clases y módulos que serán buscados
en el orden de búsqueda
ancestors
(definido en la clase Module) de una clase o módulo retorna la lista de clases y módulos
que serán buscados después que la búsqueda por ámbito fracase
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
Kernel
es un ancestor de Object:
[11] pry(main)> Object.ancestors => [Object, PP::ObjectMixin, Kernel, BasicObject]Las constantes definidas en
Kernel
pueden ser sobreescritas
por constantes al nivel mas alto, ya que estan se definen implícitamente en Object
Casiano Rodriguez León 2015-01-07