# Namespacing my classes

**URL:** <https://www.ruby-forum.com/t/namespacing-my-classes/150808>\
**Category:** Ruby\
**Created:** [October 13, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/namespacing-my-classes/150808 "2008-10-13T16:02:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 13, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/namespacing-my-classes/150808/1 "2008-10-13T16:02:05Z")

</div>

Hello, as I want to avoid name clashes I tried to organise them like  
this (just a fake example, reflecting reality)

lib/mylib.rb contains  
module Mylib

module TestDatas  
FOO = “Ho yeah”  
end

require ‘somelib/tools’  
require ‘somelib/motor’  
end

somelib/tools.rb contains  
class Mylib::Tools  
end

somelib/motor.rb contains  
class Mylib::Motor

def something()  
Mylib::TestDatas.FOO  
end

end

Well, I did this by try/error until I had no more execution errors and  
having them behaving as expected but is there any caveat ?

I’m just suprised by the location of the requires statements

Thanks

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 13, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/namespacing-my-classes/150808/2 "2008-10-13T16:15:40Z")

</div>

Zouplaz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> require ‘somelib/tools’  
> def something()  
> I’m just suprised by the location of the requires statements  
> You don’t need to put these requires inside the module blocks.

Instead, you could write:

(module MyLib  
(module TestData  
(FOO = “abc”)  
end)  
end)

(module MyLib  
(class Tool  
end)  
end)

(module MyLib  
(class Motor  
(def something  
(TestData::FOO)  
end)  
end)  
end)

That is, if you avoid the (class Abc::Def::Ghi … end) form, then  
modules namespaces will be created automatically when needed.

Then, how you spread the forms over files and execute require is up to  
you and doesn’t matter much.

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 13, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/namespacing-my-classes/150808/3 "2008-10-13T18:53:20Z")

</div>

On Oct 13, 9:12 am, removed\_email\_address@domain.invalid (Pascal J. Bourguignon)  
wrote:

> (module MyLib  
> (class Motor  
> (def something  
> (TestData::FOO)  
> end)  
> end)  
> end)

What are all those parentheses about?

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 13, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/namespacing-my-classes/150808/4 "2008-10-13T21:15:42Z")

</div>

le 13/10/2008 19:44, Robert K. nous a dit:

> end
> 
> end
> 
> Btw, there is no plural of “data” in English AFAIK.

Ho, fine ! I didn’t know I could reuse Module MyLib in several place to  
enclose the classes…

About ‘data’, I trust you 😉 I wish I could find a webchat with people  
willing to speak French with me, while speaking English with them - I  
could learn a alot, but for the moment, I just found men masturbating in  
the dark :-))

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 13, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/namespacing-my-classes/150808/5 "2008-10-13T19:46:13Z")

</div>

On 13.10.2008 18:49, Yossef M. wrote:

> > end)  
> > What are all those parentheses about?  
> > Probably a Lisp fan. 🙂

$ ruby -cw \<\<XXX

> (module MyLib  
> (module TestData  
> (FOO = “abc”)  
> end)  
> end)  
> XXX  
> Syntax OK

To OP, usually this is structured a bit differently:

lib/mylib.rb contains

module MyLib

module TestData  
FOO = “Ho yeah”  
end

end

require ‘mylib/tools’  
require ‘mylib/motor’

mylib/tools.rb contains

module MyLib  
class Tools  
end  
end

mylib/motor.rb contains

module MyLib

class Motor  
def something()  
TestData::FOO  
end  
end

end

Btw, there is no plural of “data” in English AFAIK.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 13, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/namespacing-my-classes/150808/6 "2008-10-13T21:30:24Z")

</div>

On 13.10.2008 21:12, Zouplaz wrote:

> > class Tools  
> > end  
> > end
> > 
> > end
> > 
> > Btw, there is no plural of “data” in English AFAIK.
> 
> Ho, fine ! I didn’t know I could reuse Module MyLib in several place to  
> enclose the classes…

Note also that you do not need the full namespace path when referencing  
the const (see above).

> About ‘data’, I trust you 😉 I wish I could find a webchat with people  
> willing to speak French with me, while speaking English with them - I  
> could learn a alot, but for the moment, I just found men masturbating in  
> the dark :-))

Well, you never know what it is good for. 🙂

There is an IRB chat on freenode - albeit in English.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 14, 2008, 8:31am UTC](https://www.ruby-forum.com/t/namespacing-my-classes/150808/7 "2008-10-14T08:31:48Z")

</div>

On 13.10.2008 22:43, The Higgs bozo wrote:

> Robert K. wrote:
> 
> > There is an IRB chat on freenode - albeit in English.
> 
> IRB chat! Now that’s an awesome typo. Or maybe a Freudian slip? You  
> know you’re a programmer when the distinction between IRB and IRC is  
> blurred.

LOL! It was definitively unconscious. I was probably distracted. I  
leave the interpretation to you. 🙂

Cheers

robert

PS: Are you the unknown particle that they now cannot find because the  
collider at Cern is broken?

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 13, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/namespacing-my-classes/150808/8 "2008-10-13T22:46:14Z")

</div>

Robert K. wrote:

> There is an IRB chat on freenode - albeit in English.

IRB chat! Now that’s an awesome typo. Or maybe a Freudian slip? You  
know you’re a programmer when the distinction between IRB and IRC is  
blurred.
