constant name resolution and module_eval

X

Xavier Noria

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
 
R

Robert Dober

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 D -> 1
it seems that constants behave as *declared* locals in closures.
puts M::D # -> NameError
see this for contrast, it is not module_eval that makes the difference
but the closure/block semantics
M.module_eval <<-EOS
def self.const; C end
D = 1
EOS

puts M.const
puts M::D # -> 1
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.
No it is not module_eval that sees the surrounding scope it is the
closure, as you can verify with the string
form of module_eval.
I did not know that behavior for constants either, very interesting.HTH
Robert
 
X

Xavier Noria

M.module_eval <<-EOS
def self.const; C end
D = 1
EOS

puts M.const
puts M::D # -> 1
No it is not module_eval that sees the surrounding scope it is the
closure, as you can verify with the string
form of module_eval.
I did not know that behavior for constants either, very interesting.


Good point!

And with that alternative M.const gives "M" instead of "Object" as
well. Very interesting.

-- fxn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top