Modules and importing them/their namespace?

Discussion in 'Ruby' started by Markus Fischer, Jun 2, 2009.

  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
     
    Markus Fischer, Jun 2, 2009
    #1
    1. Advertisements

  2. 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
     
    Stefano Crocco, Jun 2, 2009
    #2
    1. Advertisements

  3. Hi,

    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
     
    Markus Fischer, Jun 2, 2009
    #3
  4. 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
     
    Stefano Crocco, Jun 2, 2009
    #4
  5. Hi,

    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
     
    Markus Fischer, Jun 2, 2009
    #5
  6. Markus Fischer

    Gary Wright Guest

    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
     
    Gary Wright, Jun 2, 2009
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.