calling my framework

Discussion in 'Ruby' started by Mario Ruiz, Nov 22, 2007.

  1. Mario Ruiz

    Mario Ruiz Guest

    I'm developing a framework but I would like to call the classes as I was
    in Java.
    An example:
    requiere 'myAclasses'
    requiere 'myBclasses'
    vA=myclass22.new()
    vB=myclass55.new()

    The problem is that I don't know where are these classes and I would
    like to call them something like this:
    vA=myAclasses.myclass22.new()
    vB=myBclasses.myclass55.new()

    Is this possible???
    How can I do it?

    Thans in advance.
    --
    Posted via http://www.ruby-forum.com/.
    Mario Ruiz, Nov 22, 2007
    #1
    1. Advertising

  2. On Nov 22, 2007 2:12 PM, Mario Ruiz <> wrote:
    > I'm developing a framework but I would like to call the classes as I was
    > in Java.
    > An example:
    > requiere 'myAclasses'
    > requiere 'myBclasses'
    > vA=myclass22.new()
    > vB=myclass55.new()
    >
    > The problem is that I don't know where are these classes and I would
    > like to call them something like this:
    > vA=myAclasses.myclass22.new()
    > vB=myBclasses.myclass55.new()
    >
    > Is this possible???
    > How can I do it?


    Note that true class names has to start with an uppercase letter; I've
    compensated for that below.

    First, you can do module wrapping (namespacing):

    module MyAclasses
    class Myclass22
    end
    end

    and call as
    MyAclasses::Myclass22.new

    Second, you could do this using a method that returns a class object.

    In other words, something like

    class MyAClassContainer
    def myclass22
    return MyClass22
    end
    end
    myAclasses = MyAClassContainer.new # (or any other factory method)
    mya = myAclasses.myclass22.new

    The latter technique can be useful if your hierarchy and what you want
    to produce change at runtime. It seems like very many levels of
    indirection, though, so I would think carefully of whether it is
    necessary. (Each level of indirection tends to make things harder to
    grasp/think about/debug.)

    Eivind.
    Eivind Eklund, Nov 23, 2007
    #2
    1. Advertising

  3. Mario Ruiz

    Mario Ruiz Guest

    The first seems to be the solution to my problem, I didn't realize
    before.
    Even you can add more modules:
    module Mod1
    module Mod2
    class Clas1
    end Class
    end
    end

    a=Mod1::Mod2::Clas1.new()

    That's great!!!

    Thank you very much.
    --
    Posted via http://www.ruby-forum.com/.
    Mario Ruiz, Nov 23, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anatoly Volodko
    Replies:
    1
    Views:
    2,080
    Mattias Sjögren
    Aug 14, 2003
  2. Charles A. Lackman
    Replies:
    1
    Views:
    1,330
    smith
    Dec 8, 2004
  3. Mark
    Replies:
    4
    Views:
    1,708
    Juan T. Llibre
    Nov 17, 2005
  4. moi
    Replies:
    3
    Views:
    4,041
    quaiser_ali
    Sep 26, 2008
  5. Replies:
    2
    Views:
    9,300
    Darryl L. Pierce
    Sep 11, 2005
Loading...

Share This Page