# Constant lookup

**URL:** <https://www.ruby-forum.com/t/constant-lookup/165342>\
**Category:** Ruby\
**Created:** [April 12, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/constant-lookup/165342 "2009-04-12T20:13:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [April 12, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/constant-lookup/165342/1 "2009-04-12T20:13:43Z")

</div>

I’ve come across an odd issue when using a constant that is defined  
within more than one module namespace. I’ve searched this  
forum/ml/newsgroup for some answers, but I didn’t come across anything  
that quite addressed the situation I’m seeing.

Can anyone explain why this occurs? Is it supposed to? It doesn’t make a  
ton of sense to me.

> module Z  
> class A  
> def initialize  
> puts “Z::A”  
> end  
> end  
> module Y  
> module X  
> class A  
> def initialize  
> puts “Z::Y:❌:A”  
> end  
> end  
> end  
> class B  
> include X  
> def initialize  
> puts “Z::Y::B”  
> @a = A.new  
> end  
> end  
> end  
> end
> 
> Z::A.new  
> Z::A  
> =\> #\<Z:🅰0x573dc\>
> 
> Z::Y:❌:A.new  
> Z::Y:❌:A  
> =\> #\<Z::Y:❌🅰0x5030c\>
> 
> Z::Y::B.new  
> Z::Y::B  
> Z::A  
> =\> #\<Z::Y:🅱0x48e18 @a=#\<Z:🅰0x48dc8\>\>
> 
> Z::Y::B.const\_get(“A”)  
> =\> Z::Y:❌:A

> module Z  
> module Y  
> class B  
> const\_get(“A”)  
> end  
> end  
> end  
> =\> Z::Y:❌:A
> 
> module Z  
> module Y  
> class B  
> A  
> end  
> end  
> end  
> =\> Z::A

It seems to me, though obviously I’m wrong, that the bare constant ‘A’  
should evaluate to exactly the same thing as self.const\_get(“A”).

In the mean time, I’ll just rename my constants to non-conflicting  
names, but… why is this working this way in the first place?
