Modules and importing them/their namespace?

M

Markus Fischer

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
 
S

Stefano Crocco

|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

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
 
M

Markus Fischer

Hi,

Stefano said:
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
 
S

Stefano Crocco

|Hi,
|
|Stefano Crocco 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

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
 
M

Markus Fischer

Hi,

Stefano said:
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
 
G

Gary Wright

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 Wright
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top