Forum: Ruby Modules and importing them/their namespace?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee2809522b2e56d0d6b656486bc5e0db?d=identicon&s=25 Markus Fischer (Guest)
on 2009-06-02 13:36
(Received via mailing list)
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
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2009-06-02 13:53
(Received via mailing list)
On Tuesday 02 June 2009, Markus Fischer 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
Ee2809522b2e56d0d6b656486bc5e0db?d=identicon&s=25 Markus Fischer (Guest)
on 2009-06-02 14:24
(Received via mailing list)
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
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2009-06-02 14:44
(Received via mailing list)
On Tuesday 02 June 2009, Markus Fischer 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
Ee2809522b2e56d0d6b656486bc5e0db?d=identicon&s=25 Markus Fischer (Guest)
on 2009-06-02 15:01
(Received via mailing list)
Hi,

Stefano Crocco 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
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2009-06-02 16:43
(Received via mailing list)
On Jun 2, 2009, at 9:00 AM, Markus Fischer 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 Wright
This topic is locked and can not be replied to.