# Including a namespace for just one class

**URL:** <https://www.ruby-forum.com/t/including-a-namespace-for-just-one-class/80389>\
**Category:** Ruby\
**Created:** [December 25, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/including-a-namespace-for-just-one-class/80389 "2006-12-25T18:21:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [December 25, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/including-a-namespace-for-just-one-class/80389/1 "2006-12-25T18:21:58Z")

</div>

I would like to reference constants and classes defined in module  
People in the class Schedule.

class Login is not part of any module (other than the root).

doing this:  
class Schedule  
include People  
def a\_method  
# Worker.new won’t work  
People::Worker.new # works  
end  
end

doesn’t work.

Is there a way to include the module People so that I don’t need to  
prefix People:: for only one particular class?

Also, why doesn’t what I tried work? Doesn’t include copy in the  
constants, and aren’t classes just constants?

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [December 25, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/including-a-namespace-for-just-one-class/80389/2 "2006-12-25T20:46:09Z")

</div>

On Dec 25, 2006, at 12:20 PM, S. Robert J. wrote:

> ```
> People::Worker.new # works
> 
> ```
> 
> end  
> end
> 
> Is there a way to include the module People so that I don’t need to  
> prefix People:: for only one particular class?

I don’t know why it doesn’t work for you – it works fine for me.

`
module People
    class Worker
    end
end
class Schedule
include People
def a_method
p Worker.new
end
end
Schedule.new.a_method
` #

Regards, Morton

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [December 25, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/including-a-namespace-for-just-one-class/80389/3 "2006-12-25T21:20:18Z")

</div>

On Dec 25, 2006, at 12:20 PM, S. Robert J. wrote:

> I would like to reference constants and classes defined in module  
> People in the class Schedule.

Here’s an example that works fine for me:  
module People  
POPULATION = 1234567  
end

class Schedule  
include People  
def x  
p POPULATION  
end  
end

s = Schedule.new  
s.x  
#=\> 1234567

p VERSION  
#=\> “1.8.5”

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [December 25, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/including-a-namespace-for-just-one-class/80389/4 "2006-12-25T23:16:35Z")

</div>

S. Robert J. wrote:

> Is it possible that Rails changes things somehow?  
> I think I know - the autoloader can’t find them then!

Does that make any sense? The autoloader can find them if I include  
People in the global namespace, but not if I include it in the class?!  
If so, is there a workaround?

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [December 25, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/including-a-namespace-for-just-one-class/80389/5 "2006-12-25T22:30:29Z")

</div>

Is it possible that Rails changes things somehow?  
I think I know - the autoloader can’t find them then!

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [December 28, 2006, 6:23am UTC](https://www.ruby-forum.com/t/including-a-namespace-for-just-one-class/80389/6 "2006-12-28T06:23:05Z")

</div>

On Tue, Dec 26, 2006 at 07:15:04AM +0900, S. Robert J. wrote:

> S. Robert J. wrote:
> 
> > Is it possible that Rails changes things somehow?  
> > I think I know - the autoloader can’t find them then!
> 
> Does that make any sense? The autoloader can find them if I include  
> People in the global namespace, but not if I include it in the class?!  
> If so, is there a workaround?

Yes it makes sense and you are probably right. The workaround is to  
explicitly require the file

e.g:  
require ‘people’ # Or whatever the file is
