Modules and importing them/their namespace?


#1

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

#2

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


#3

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

#4

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


#5

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.


#6

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