From: Xavier Noria Date: 2007-11-16T17:49:30+09:00 Subject: constant name resolution and module_eval I'm trying to understand well the rules for constant name resolution. By now what I have is: (1) A constant is first looked up in the current module, then recursively in its mixins (ancestors). Then go up to the parent, and iterate up to the mixins of Object. (2) When you resolve a relative path Foo::Bar first you resolve the constant Foo using the rule above. If found, you look for Bar in Foo. There's no backtracking if that choice for Foo fails. If that's right, I need yet to refine what's the "current module". From my trials looks like the current module is determined by "module/ class" explicit keyword nesting, and module_eval does not change it: C = "Object" module M C = "M" end M.module_eval do def self.const; C end D = 1 end puts M.const puts M::D # -> NameError But surprisingly module_eval defines M.const, and does _not_ define M::D. I know module_eval sees the surrounding scope and that allows some idioms a regular module reopening does not, but I need yet to put the pieces together for constants. Is that there are two kinds of "current module"s, one for constant lookup, and another one for method definitions, which happen to match when you use the "module/class" keyword nesting? My trials suggest that's the case but I'd like someone with a better understanding of Ruby internals to confirm it and perhaps define more clearly those two "current" modules at any given point of the program. -- fxn