# Constant Resolution Confusion

**URL:** <https://www.ruby-forum.com/t/constant-resolution-confusion/123434>\
**Category:** Ruby\
**Created:** [December 17, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/constant-resolution-confusion/123434 "2007-12-17T19:11:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 17, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/constant-resolution-confusion/123434/1 "2007-12-17T19:11:13Z")

</div>

I was surprised by the following this weekend:

module Foo  
module Bar; end  
class Whee1  
include Bar  
end  
end

# All is well

class Foo::Whee2  
include Bar  
end  
#=\> uninitialized constant Foo::Whee2::Bar (NameError)

Is the answer simply “lexically scoped resolution”?

Is the above still the case with 1.9?

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 17, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/constant-resolution-confusion/123434/2 "2007-12-17T19:33:33Z")

</div>

On Tue, 2007-12-18 at 03:10 +0900, Phrogz wrote:

> class Foo::Whee2  
> include Bar  
> end  
> #=\> uninitialized constant Foo::Whee2::Bar (NameError)
> 
> Is the answer simply “lexically scoped resolution”?
> 
> Is the above still the case with 1.9?

Yuck…  
And yes “problem” persists with Ruby1.9.

–  
Let them talk of their oriental summer climes of everlasting  
conservatories; give me the privilege of making my own summer with my  
own coals.

[http://gnufied.org](http://gnufied.org)

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 17, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/constant-resolution-confusion/123434/3 "2007-12-17T19:54:04Z")

</div>

On Dec 17, 12:07 pm, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Foo::Whee2  
> include Bar  
> end  
> #=\> uninitialized constant Foo::Whee2::Bar (NameError)
> 
> Is the answer simply “lexically scoped resolution”?
> 
> Is the above still the case with 1.9?

Maybe it’s because (I assume) this is a paired-down example, but in  
the example class Whee1 is in the scope of module Foo, and so is  
module Bar, but module Bar isn’t in the scope of class Foo::Whee2, so  
you have to include it as qualified Foo::Bar. It doesn’t look like  
module Bar is lexically scoped to class Foo.

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 17, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/constant-resolution-confusion/123434/4 "2007-12-17T20:59:17Z")

</div>

On Dec 17, 11:47 am, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > end
> > 
> > ```
> > 
> > Is the above still the case with 1.9?
> 
> Maybe it’s because (I assume) this is a paired-down example, but in  
> the example class Whee1 is in the scope of module Foo, and so is  
> module Bar, but module Bar isn’t in the scope of class Foo::Whee2, so  
> you have to include it as qualified Foo::Bar. It doesn’t look like  
> module Bar is lexically scoped to class Foo.

The actual situation was that I started writing code all nested as in  
the first example. Everything was working, my unit tests were passing,  
all was right with the world.

But I didn’t like the nesting. The extra level of folding was getting  
in the way, and the non-explicit nesting made my function pop up not  
the way I wanted.

So I changed the code in a way that I thought was purely formatting; I  
pulled the classes (and modules) out to the top level with an explicit  
Foo:: prefix for each. And I was surprised when my code stopped  
working.

It was simple enough to see the error, and not very painful to find  
all the cases where I had included the module and also add the Foo::  
prefix. But it surprised me. Despite likely reading about this issue  
previously, I had thought constant resolution would not be affected by  
that particular edit.

_shrug_
