# Namespaces a la C#

**URL:** <https://www.ruby-forum.com/t/namespaces-a-la-c/157443>\
**Category:** Ruby\
**Created:** [January 11, 2009, 11:33am UTC](https://www.ruby-forum.com/t/namespaces-a-la-c/157443 "2009-01-11T11:33:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joeytempest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@joeytempest](https://www.ruby-forum.com/u/joeytempest)\
**Post date:** [January 11, 2009, 11:33am UTC](https://www.ruby-forum.com/t/namespaces-a-la-c/157443/1 "2009-01-11T11:33:44Z")

</div>

Hei everybody,

```
 I'm kind of new to ruby and, since i come from C#, one of the main

```

things i’m missing is the .NET “project” way of organizing the codebase.  
As you probably know: in .NET you can arrange your code into a project,  
then reference that project to have access to everything public inside  
it. Example:

namespace Temp  
{  
public class TestClass { }  
}

namespace Temp { public class TestTwoClass { } }

then, in another project, you get access to TestClass and TestTwoClass  
referencing the DotNetProject and you can simplify the syntax too by  
adding ‘using’ directives on your files like:

using Temp;

public class NewClass  
{  
public void DoSomething()  
{  
TestClass ts = new TestClass();  
TestTwoClass ts2 = new TestTwoClass();  
//the following is still legal and  
//it’s the syntax you’ll have to use  
//if you choose not to add the ‘using’ line  
//at the top  
Temp.TestClass ts3 = new Temp.TestClass();  
Temp.TestTwoClass ts4 = new Temp.TestTwoClass();  
}  
}

So: is there a way to do something similar in Ruby? Like using a Module,  
but splitted on separate files, such as :

Module test Class testclass end end Module test Class testtwoclass end end

and then

require 'test' ... ts = test::testclass.new ts2 = test::testtwoclass.new ...

Sorry if this is a lame question but the  
add-every-freaking-file-every-freaking-time way isn’t just going to work  
on a large project and i don’t really like having to code every class  
belonging to a namespace in the same file like this :

Module test  
Class one  
end

```
Class two
end
...
Class n
end

```

end

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![joeytempest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@joeytempest](https://www.ruby-forum.com/u/joeytempest)\
**Post date:** [January 11, 2009, 12:08pm UTC](https://www.ruby-forum.com/t/namespaces-a-la-c/157443/2 "2009-01-11T12:08:06Z")

</div>

Cippo L. schrieb:

> Module test  
> ts2 = test::testtwoclass.new  
> …  
> this should work perfectly (if you make the Modulename uppercase).  
> If you don’t want to type the “Test::” thingy use “include Test” in  
> bootstrapper.rb  
> And if you wan’t to just require ‘test’, the best way is something like  
> this:  
>   
> Dir[“test/\*.rb”].each { |file| require file }

---

<div class="post-metadata">

**Author:** ![joeytempest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@joeytempest](https://www.ruby-forum.com/u/joeytempest)\
**Post date:** [January 11, 2009, 12:19pm UTC](https://www.ruby-forum.com/t/namespaces-a-la-c/157443/3 "2009-01-11T12:19:13Z")

</div>

On Sun, Jan 11, 2009 at 7:33 PM, Cippo L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> TestClass ts = new TestClass();
> 
> ```
> 
> So: is there a way to do something similar in Ruby? Like using a Module,  
> Class testtwoclass  
> …  
> Class two  
> end  
> …  
> Class n  
> end  
> end
> 
> Thanks in advance!

The short answer is: just try it 🙂  
The long answer is: Ruby lets you reopen just about everything, so you  
can do this without problems:

module Foo  
class Bar  
end  
end

module Foo  
class Duh  
end  
end

It doesn’t matter in which files the code is, as long as you require  
both.  
The way libraries do it is usually something like this:  
[http://paste.linuxhelp.tv/pastes/view/15344](http://paste.linuxhelp.tv/pastes/view/15344)

Now you can reference Zoo::Animal and Zoo::Panther as long as you  
require the /lib/zoo.rb
