# Module MyMod::MyOtherMod question

**URL:** <https://www.ruby-forum.com/t/module-mymod-myothermod-question/195817>\
**Category:** Ruby\
**Created:** [September 30, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/module-mymod-myothermod-question/195817 "2010-09-30T18:01:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mydarkpassenger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mydarkpassenger](https://www.ruby-forum.com/u/mydarkpassenger)\
**Post date:** [September 30, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/module-mymod-myothermod-question/195817/1 "2010-09-30T18:01:49Z")

</div>

Can someone please explain what the difference is?

A assumed these were synonymous, but they are not:

module A  
module B  
…  
end  
end

Not equivalent to?:

module A::B  
…  
end

Thanks alot.

---

<div class="post-metadata">

**Author:** ![mydarkpassenger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mydarkpassenger](https://www.ruby-forum.com/u/mydarkpassenger)\
**Post date:** [September 30, 2010, 6:07pm UTC](https://www.ruby-forum.com/t/module-mymod-myothermod-question/195817/2 "2010-09-30T18:07:06Z")

</div>

Gene A. wrote:

> [I] assumed these were synonymous, but they are not:

I stand corrected. It appears that in order to use the following syntax:

module A::B  
…  
end

module A must be defined.

---

<div class="post-metadata">

**Author:** ![mydarkpassenger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mydarkpassenger](https://www.ruby-forum.com/u/mydarkpassenger)\
**Post date:** [September 30, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/module-mymod-myothermod-question/195817/3 "2010-09-30T19:12:46Z")

</div>

Gene A. wrote:

> Gene A. wrote:
> 
> > [I] assumed these were synonymous, but they are not:
> 
> I stand corrected. It appears that in order to use the following syntax:
> 
> module A::B  
> …  
> end
> 
> module A must be defined.

Yes. But even then,

module A; end  
module A::B  
…  
end

is different to

module A  
module B  
…  
end  
end

in the way that constants are looked up.

> > module A; FOO = 123; end  
> > =\> 123
> 
> > module A::B  
> > puts FOO  
> > end  
> > NameError: uninitialized constant A:🅱:FOO  
> > from (irb):3  
> > from :0
> 
> > module A; module B  
> > puts FOO  
> > end; end  
> > 123  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![mydarkpassenger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mydarkpassenger](https://www.ruby-forum.com/u/mydarkpassenger)\
**Post date:** [October 1, 2010, 12:27am UTC](https://www.ruby-forum.com/t/module-mymod-myothermod-question/195817/4 "2010-10-01T00:27:18Z")

</div>

The constant that classes are assigned to when defined may be prefixed  
by the names of existing modules using the scope operator (::). When  
this happens, the leading scope operator places the class or module in  
the top-level scope.

Here is the example:

CONST = ‘outer’

module A  
CONST = ‘inner’  
end

module A  
class B  
def self.get\_const  
CONST  
end  
end  
end

p A::B.get\_const # =\> inner

Now, class B is inserted into module A’s namespace, but it’s not  
interpreted in the context of A. As a result, the reference to CONST  
resolves to the top-level constant, not A’s version.

class A::B  
def self.get\_const  
CONST  
end  
end

p A::B.get\_const # =\> outer
