# Using of module and "Is not missing constant" error

**URL:** <https://www.ruby-forum.com/t/using-of-module-and-is-not-missing-constant-error/186000>\
**Category:** Ruby\
**Created:** [March 30, 2010, 9:47am UTC](https://www.ruby-forum.com/t/using-of-module-and-is-not-missing-constant-error/186000 "2010-03-30T09:47:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sorokyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@sorokyn](https://www.ruby-forum.com/u/sorokyn)\
**Post date:** [March 30, 2010, 9:47am UTC](https://www.ruby-forum.com/t/using-of-module-and-is-not-missing-constant-error/186000/1 "2010-03-30T09:47:13Z")

</div>

Hi,

I have some trouble using module. I’m working on a rails project, but I  
think it’s concerning Ruby in general.

I get this kind of error ‘Module is not missing constant Klass!’

I have folders looking like this :  
x/y/klass.rb.

In klass my code is looking something like this :  
module x  
module y  
class klass  
end  
end  
end

In another file :  
module x  
class Anotherklass  
#some method  
end  
end

When I use once in my methode y::klass everything works fine.  
But if I use it twice, the seconde line raise the error : “x is not  
missing constant klass”.

I tried to load manually the file with require call but it doesn’t  
change anything.  
It’s like after the first call, ruby know that Klass is nested into y  
but when I use only Klass, ruby raise that he doesn’t know Klass …

I really dunno what to do to solve this. I’m looking arround require and  
include.

Thanks for reading

---

<div class="post-metadata">

**Author:** ![sorokyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@sorokyn](https://www.ruby-forum.com/u/sorokyn)\
**Post date:** [March 30, 2010, 10:09am UTC](https://www.ruby-forum.com/t/using-of-module-and-is-not-missing-constant-error/186000/2 "2010-03-30T10:09:11Z")

</div>

On Tue, Mar 30, 2010 at 1:47 AM, Benoit M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> In klass my code is looking something like this :  
> #some method  
> but when I use only Klass, ruby raise that he doesn’t know Klass …
> 
> I really dunno what to do to solve this. I’m looking arround require and  
> include.
> 
> ## Thanks for reading
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Classes and modules are constants, so their names need to begin with  
Uppercase Letters.

---

<div class="post-metadata">

**Author:** ![sorokyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@sorokyn](https://www.ruby-forum.com/u/sorokyn)\
**Post date:** [March 30, 2010, 10:15am UTC](https://www.ruby-forum.com/t/using-of-module-and-is-not-missing-constant-error/186000/3 "2010-03-30T10:15:16Z")

</div>

Josh C. wrote:

> Classes and modules are constants, so their names need to begin with  
> Uppercase Letters.

Yes, sorry for my exemple, my real code follow of course the naming  
style.

---

<div class="post-metadata">

**Author:** ![sorokyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@sorokyn](https://www.ruby-forum.com/u/sorokyn)\
**Post date:** [March 30, 2010, 12:41pm UTC](https://www.ruby-forum.com/t/using-of-module-and-is-not-missing-constant-error/186000/4 "2010-03-30T12:41:54Z")

</div>

Benoit M. wrote:

> Josh C. wrote:
> 
> > Classes and modules are constants, so their names need to begin with  
> > Uppercase Letters.
> 
> Yes, sorry for my exemple, my real code follow of course the naming  
> style.

So show a real piece of Ruby code which replicates the problem -  
something we can paste into a file and run for ourselves.

Often, in boiling down a problem into a simple test case like that,  
you’ll discover what the problem is. And if you don’t, you will have  
made it much easier for others to determine what the problem is.

---

<div class="post-metadata">

**Author:** ![sorokyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@sorokyn](https://www.ruby-forum.com/u/sorokyn)\
**Post date:** [March 30, 2010, 3:16pm UTC](https://www.ruby-forum.com/t/using-of-module-and-is-not-missing-constant-error/186000/5 "2010-03-30T15:16:16Z")

</div>

On Tue, Mar 30, 2010 at 5:47 AM, Benoit M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > something we can paste into a file and run for ourselves.  
> > def index  
> > def self.list  
> > into already loaded…  
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I think this is going to be a Rails question about how Rails handles  
loading  
files / environments.

But a few thoughts to look at:

What happens if you say Local::VerticalBuilder::Vertical.list ?

What happens if you run your server in production mode?

What happens if you place the code into an initializer instead of lib?

---

<div class="post-metadata">

**Author:** ![sorokyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@sorokyn](https://www.ruby-forum.com/u/sorokyn)\
**Post date:** [March 30, 2010, 1:47pm UTC](https://www.ruby-forum.com/t/using-of-module-and-is-not-missing-constant-error/186000/6 "2010-03-30T13:47:52Z")

</div>

Brian C. wrote:

> Benoit M. wrote:
> 
> > Josh C. wrote:
> > 
> > > Classes and modules are constants, so their names need to begin with  
> > > Uppercase Letters.
> > 
> > Yes, sorry for my exemple, my real code follow of course the naming  
> > style.
> 
> So show a real piece of Ruby code which replicates the problem -  
> something we can paste into a file and run for ourselves.
> 
> Often, in boiling down a problem into a simple test case like that,  
> you’ll discover what the problem is. And if you don’t, you will have  
> made it much easier for others to determine what the problem is.  
> Sure.

I create a test app with this code.  
In app/controller/local/class\_generator\_controller.rb :

class Local::VerticalGeneratorController \< ApplicationController  
def index  
@models = VerticalBuilder::Vertical.list  
@models = VerticalBuilder::Vertical.list #second call raise an  
exception  
end  
end

In lib/local/vertical\_builder/vertical.rb :  
module Local  
module VerticalBuilder  
class Vertical  
def self.list  
[]  
end  
end  
end  
end

I get the same error when I call  
[http://localhost:3000/local/vertical\_generator](http://localhost:3000/local/vertical_generator) :  
Local is not missing constant VerticalBuilder!  
It seams that Ruby try to load each time the Constant instead of look  
into already loaded…

---

<div class="post-metadata">

**Author:** ![sorokyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@sorokyn](https://www.ruby-forum.com/u/sorokyn)\
**Post date:** [March 30, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/using-of-module-and-is-not-missing-constant-error/186000/7 "2010-03-30T15:47:30Z")

</div>

Josh C. wrote:

> But a few thoughts to look at:  
> What happens if you say Local::VerticalBuilder::Vertical.list ?  
> It Works !

But I really would like to know why.  
My class is called Local::Class name, so it belongs to the module Local  
and must be able to access to all submodules, isn’t it ?

The strangest thing is that without this Local:: the call work once only  
like I said.  
Really strange …

Anyway, thanks for help. I can continue my project with this now.  
If anyone get info about this freaky trick I would like to see the light  
of truth 😛

---

<div class="post-metadata">

**Author:** ![sorokyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@sorokyn](https://www.ruby-forum.com/u/sorokyn)\
**Post date:** [March 30, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/using-of-module-and-is-not-missing-constant-error/186000/8 "2010-03-30T16:44:03Z")

</div>

> I create a test app with this code.  
> In app/controller/local/class\_generator\_controller.rb :
> 
> class Local::VerticalGeneratorController \< ApplicationController  
> def index  
> @models = VerticalBuilder::Vertical.list  
> @models = VerticalBuilder::Vertical.list #second call raise an  
> exception  
> end  
> end

Try rewriting this as:

module Local  
class VerticalGeneratorController \< ApplicationController  
…  
end  
end

I have seen similar constant lookup problems before.

$ irb --simple-prompt

> > class Foo  
> > ZZZ = 123  
> > end  
> > =\> 123
> 
> > class Foo::Bar  
> > def baz  
> > puts ZZZ  
> > end  
> > end  
> > =\> nil
> 
> > Foo::Bar.new.baz  
> > NameError: uninitialized constant Foo::Bar::ZZZ  
> > from (irb):6:in `baz’  
> > from (irb):9  
> > from :0

Compare:

$ irb --simple-prompt

> > class Foo  
> > ZZZ = 123  
> > end  
> > =\> 123
> 
> > class Foo  
> > class Bar  
> > def baz  
> > puts ZZZ  
> > end  
> > end  
> > end  
> > =\> nil
> 
> > Foo::Bar.new.baz  
> > 123  
> > =\> nil
