# Module#constants

**URL:** <https://www.ruby-forum.com/t/module-constants/120135>\
**Category:** Ruby\
**Created:** [November 14, 2007, 1:28am UTC](https://www.ruby-forum.com/t/module-constants/120135 "2007-11-14T01:28:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [November 14, 2007, 1:28am UTC](https://www.ruby-forum.com/t/module-constants/120135/1 "2007-11-14T01:28:13Z")

</div>

According to my trials nor Module#constants neither Module#const\_get  
work with constants defined in enclosing modules:

module M  
C = 1  
module N  
C # -\> 1  
end  
end

M::N.constants # -\> []  
M::N.const\_get(“C”) # -\> NameError

Looks as if they only took into account ancestors.

But the Pickaxe says that Module#constants:

“Returns an array of the names of the constants accessible in mod.  
This includes the names of constants in any included modules (example  
at start of section).”

Does “This includes” mean “These are”? Is there a way to obtain “M::C”  
as qualified name of a constant visible by N?

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [November 14, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/module-constants/120135/2 "2007-11-14T13:44:52Z")

</div>

On Nov 14, 2007, at 1:27 AM, Xavier N. wrote:

> M::N.constants # -\> []  
> M::N.const\_get(“C”) # -\> NameError
> 
> Looks as if they only took into account ancestors.

I gues that C is somehow in scope, but there are other examples that  
puzzle me:

module M  
C = 1  
end

module PutSomethingInBetweenJustInCase  
end

module M  
module N  
puts C # -\> 1  
end  
end

Why is M::C in scope there?

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [November 14, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/module-constants/120135/3 "2007-11-14T18:37:02Z")

</div>

On Nov 14, 2007, at 5:44 AM, Xavier N. wrote:

> Why is M::C in scope there?

for the same reason that File and Object are. imagine if C were not,  
then we would have to write

::File.open path

whenever opening a File.

the scope crawls up the nesting.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [November 15, 2007, 1:05am UTC](https://www.ruby-forum.com/t/module-constants/120135/4 "2007-11-15T01:05:21Z")

</div>

On Nov 14, 2007, at 9:41 PM, Xavier N. wrote:

> module X  
> module N  
> C  
> end  
> end

Nah, that didn’t add information. When the code is interpreted and we  
arrive to “module X” the X constant is looked up and has a module as  
value, which happens to be the same module object that is stored under  
M. So that example is no different from plain

module M  
module N  
C  
end  
end

This test brings some light in what I think I don’t understand:

module M  
C = 1  
module N  
end  
end

class Module  
def const\_missing©  
puts self  
end  
end

M::N.module\_eval do  
C  
puts self  
end

Gives:

Object  
M::N

So I think I am narrowing this to how scope works. I know module\_eval  
sees stuff from the surrounding scope, but I think I have a  
misunderstanding between “scope” and “evaluating the block in the  
context of M::N”.

There’s some module that’s taken as a starting point to crawl up in  
the namespace for constant resolution (Object), and there’s a  
different module to which method definitions _but not constant  
definitions_ apply (the remark about constants is not proven in the  
example).

Can anyone define those “two” modules better?

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [November 14, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/module-constants/120135/5 "2007-11-14T21:42:20Z")

</div>

On Nov 14, 2007, at 6:36 PM, ara.t.howard wrote:

> whenever opening a File.
> 
> the scope crawls up the nesting.

Thank you! I had once that hypothesis but this example broke it:

module M  
C = 1  
module N  
end  
end

M::N.module\_eval do  
File # -\> OK  
C # -\> NameError  
end

File is seen but C is not. So I think there’s something more into it.  
The best conjecture I have by now is that you need to have the  
enclosing module syntactically there with the nested "module/class"s.

However, I have seen that even if there something syntactic the name  
of the constant is not used, I mean this works:

module M  
C = 1  
module N  
end  
end

X = M  
module X  
module N  
C  
end  
end

That makes some sense because if X is already a module that module is  
reponed, fine, but all the examples together give kind of a mix of  
runtime and syntatic things to take into account.

I don’t yet understand the complete rule.

– fxn
