# Modules and importing them/their namespace?

**URL:** <https://www.ruby-forum.com/t/modules-and-importing-them-their-namespace/169329>\
**Category:** Ruby\
**Created:** [June 2, 2009, 1:36pm UTC](https://www.ruby-forum.com/t/modules-and-importing-them-their-namespace/169329 "2009-06-02T13:36:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [June 2, 2009, 1:36pm UTC](https://www.ruby-forum.com/t/modules-and-importing-them-their-namespace/169329/1 "2009-06-02T13:36:06Z")

</div>

Hi,

I haven’t found anything so far, so probably it doesn’t exist and I’m  
thinking to much java-ish, but is there a way to import a namespace so I  
do not need long prefixes?

Probably bad example, but if I would name  
MyModule::HasSubmodile::AndAnotherOne::FinallyMyClass , and I want to  
use it just like any other module, is there a way to import the  
“MyModule::HasSubmodile::AndAnotherOne” name and when I write  
FinallyMyClass.new it automatically resolves to the right one?

thanks,

- Markus

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [June 2, 2009, 1:53pm UTC](https://www.ruby-forum.com/t/modules-and-importing-them-their-namespace/169329/2 "2009-06-02T13:53:42Z")

</div>

On Tuesday 02 June 2009, Markus F. wrote:

> |FinallyMyClass.new it automatically resolves to the right one?  
> |  
> |thanks,  
> |- Markus

Simply create a constant which points to the module:

FinallyMyClass = MyModule::HasSubmodile::AndAnotherOne::FinallyMyClass

With this you can do:

obj = FinallyMyClass.new

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [June 2, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/modules-and-importing-them-their-namespace/169329/3 "2009-06-02T14:24:49Z")

</div>

Hi,

Stefano C. wrote:

> Simply create a constant which points to the module:
> 
> FinallyMyClass = MyModule::HasSubmodile::AndAnotherOne::FinallyMyClass
> 
> With this you can do:
> 
> obj = FinallyMyClass.new
> 
> I hope this helps

I don’t think so, as it does not make the current scope aware of  
modules/namespace. Without prefix, I just can’t access FinallyMyClass  
like I would do it in java(-ish).

Probably nothing which belongs into ruby/dynamically typed language.

thanks,

- Markus

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [June 2, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/modules-and-importing-them-their-namespace/169329/4 "2009-06-02T14:44:13Z")

</div>

On Tuesday 02 June 2009, Markus F. wrote:

> |\>  
> |\> I hope this helps  
> |  
> |I don’t think so, as it does not make the current scope aware of  
> |modules/namespace. Without prefix, I just can’t access FinallyMyClass  
> |like I would do it in java(-ish).  
> |  
> |Probably nothing which belongs into ruby/dynamically typed language.  
> |  
> |thanks,  
> |- Markus

Sorry, I think I misunderstood your question. I thought you only wanted  
to use  
a single class without prefix, but reading again your first mail, it  
seems  
that you want everything under MyModule::HasSubmodile::AndAnotherOne to  
look  
like it was in the main object (or in the module you’re working in). Is  
this  
correct? If so, you can try including the  
MyModule::HasSubmodile::AndAnotherOne module inside your own, or in the  
toplevel object, like this:

include MyModule::HasSubmodile::AndAnotherOne

This will give you access to constants and instance methods defined in  
AndAnotherOne (but not to its class methods or to constants defined in  
MyModule or HasSubmodule). I’m not completely sure this is exactly what  
you  
want, but it’s the best thing I can think of (I don’t know java, so I  
don’t  
know what exactly you expect to be able to do with this).

Stefano

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [June 2, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/modules-and-importing-them-their-namespace/169329/5 "2009-06-02T16:43:33Z")

</div>

On Jun 2, 2009, at 9:00 AM, Markus F. wrote:

> I haven’t though about that, I will look into it. Still have to get  
> used to ruby and how its scoping works with require/include.

I just wanted to point out that require and include are two very  
different beasts in Ruby. I point this out because those names were  
stumbling blocks to me when I learned Ruby and I’ve seen other people  
trip up with them also.

I think the confusion comes from trying to map C’s ‘include’ semantics  
to Ruby’s ‘include’ or ‘require’ features. This is a mistake since  
all three of those facilities are very different from one another.

Gary W.

---

<div class="post-metadata">

**Author:** ![Markus\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Markus\_F](https://www.ruby-forum.com/u/Markus_F)\
**Post date:** [June 2, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/modules-and-importing-them-their-namespace/169329/6 "2009-06-02T15:01:19Z")

</div>

Hi,

Stefano C. wrote:

> correct? If so, you can try including the  
> MyModule::HasSubmodile::AndAnotherOne module inside your own, or in the  
> toplevel object, like this:
> 
> include MyModule::HasSubmodile::AndAnotherOne

I haven’t though about that, I will look into it. Still have to get used  
to ruby and how its scoping works with require/include.

thanks!

- Markus
