Re-opened nested module anomaly in Ruby -


why re-opening nested module give different results depending on syntax used? example, works fine:

module   module e   end end module   module e     def e.e     end   end end 

but this:

module   module e   end end module a::e   def e.e   end end 

gives error:

reopen.rb:6:in `<module:e>': uninitialized constant a::e::e (nameerror) reopen.rb:5:in `<main>' 

(before points out, workaround use self instead of module name when defining e.e, that's not point of post.)

the module keyword sets namespace context checked references existing names of modules. these namespaces searched inner-to-outer resolve references module (and class) names.

in first example, looks may need define e.e inside module e block, in fact don't:

module   module e   end end module   def e.e   end end 

what happens in both examples ruby looks @ current namespace, , tries <namespace>::e module name. in both examples, first thing checks in fact a::e::e not exist. falls next context. examples differ: in first example a::e valid, in second example, e not. error throws relates first name checked.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -