Namespace conflict resolution

Discussion in 'Ruby' started by Dema, Jul 12, 2005.

  1. Dema

    Dema Guest

    Hi guys,

    This question is better explained with an example:

    I have the standard File class, right? Suppose I define another File
    class within my own namespace:

    module MyNamespace
    class File
    ...
    end
    end

    Now, if I am on an irb session and I want to refer to my
    MyNamespace::File class using only the short form 'File', I would do:
    include MyNamespace

    How does Ruby decide which class to use when I reference 'File' in this
    scope? (I've already tested and the original 'File' class is the chosen
    class)

    How would I be able to temporarely replace the standard 'File' class in
    the global Object namespace with my own File class and then revert it
    back to the standard when I'm done?

    rgds
    Dema
    http://dema.ruby.com.br
    Dema, Jul 12, 2005
    #1
    1. Advertising

  2. Dema wrote:
    > Hi guys,
    >
    > This question is better explained with an example:
    >
    > I have the standard File class, right? Suppose I define another File
    > class within my own namespace:
    >
    > module MyNamespace
    > class File
    > ...
    > end
    > end
    >
    > Now, if I am on an irb session and I want to refer to my
    > MyNamespace::File class using only the short form 'File', I would do:
    > include MyNamespace
    >
    > How does Ruby decide which class to use when I reference 'File' in this
    > scope? (I've already tested and the original 'File' class is the chosen
    > class)
    >
    > How would I be able to temporarely replace the standard 'File' class in
    > the global Object namespace with my own File class and then revert it
    > back to the standard when I'm done?
    >
    > rgds
    > Dema
    > http://dema.ruby.com.br


    My general response would be: don't do that. My personal experience is
    that slapping existing Ruby classes under your own namespace is a bad
    idea. It's just a pain. The fact that you're trying to use the "short
    form" only reinforces that it's unnecessary.

    Just put your methods directly in the File class without the added
    namespace.

    Regards,

    Dan
    Daniel Berger, Jul 12, 2005
    #2
    1. Advertising

  3. Dema

    Dema Guest

    Unfortunately, I can't do that.

    Both the class definitions and the code that's going to use the short
    form of the classes are evaluated during runtime, so it's basically a
    environment where the power user can define and use new classes.

    I want this runtime evaluation to occur in a sandbox, without
    interference or conflicts from the standard class names, but I don't
    want to obligate the user to use the long names for the namespaced
    classes.

    I hope I am being clear here.

    rgds
    Dema

    Daniel Berger wrote:
    > Dema wrote:
    > > Hi guys,
    > >
    > > This question is better explained with an example:
    > >
    > > I have the standard File class, right? Suppose I define another File
    > > class within my own namespace:
    > >
    > > module MyNamespace
    > > class File
    > > ...
    > > end
    > > end
    > >
    > > Now, if I am on an irb session and I want to refer to my
    > > MyNamespace::File class using only the short form 'File', I would do:
    > > include MyNamespace
    > >
    > > How does Ruby decide which class to use when I reference 'File' in this
    > > scope? (I've already tested and the original 'File' class is the chosen
    > > class)
    > >
    > > How would I be able to temporarely replace the standard 'File' class in
    > > the global Object namespace with my own File class and then revert it
    > > back to the standard when I'm done?
    > >
    > > rgds
    > > Dema
    > > http://dema.ruby.com.br

    >
    > My general response would be: don't do that. My personal experience is
    > that slapping existing Ruby classes under your own namespace is a bad
    > idea. It's just a pain. The fact that you're trying to use the "short
    > form" only reinforces that it's unnecessary.
    >
    > Just put your methods directly in the File class without the added
    > namespace.
    >
    > Regards,
    >
    > Dan
    Dema, Jul 12, 2005
    #3
  4. You can evaluate the code to be run in a sandbox in the binding of a
    module you defined your own File class in (see eval, module_eval). To
    refer to a standard class you will have to use ::File from there.

    Gennady.

    Dema wrote:
    > Unfortunately, I can't do that.
    >
    > Both the class definitions and the code that's going to use the short
    > form of the classes are evaluated during runtime, so it's basically a
    > environment where the power user can define and use new classes.
    >
    > I want this runtime evaluation to occur in a sandbox, without
    > interference or conflicts from the standard class names, but I don't
    > want to obligate the user to use the long names for the namespaced
    > classes.
    >
    > I hope I am being clear here.
    >
    > rgds
    > Dema
    >
    > Daniel Berger wrote:
    >
    >>Dema wrote:
    >>
    >>>Hi guys,
    >>>
    >>>This question is better explained with an example:
    >>>
    >>>I have the standard File class, right? Suppose I define another File
    >>>class within my own namespace:
    >>>
    >>>module MyNamespace
    >>> class File
    >>> ...
    >>> end
    >>>end
    >>>
    >>>Now, if I am on an irb session and I want to refer to my
    >>>MyNamespace::File class using only the short form 'File', I would do:
    >>>include MyNamespace
    >>>
    >>>How does Ruby decide which class to use when I reference 'File' in this
    >>>scope? (I've already tested and the original 'File' class is the chosen
    >>>class)
    >>>
    >>>How would I be able to temporarely replace the standard 'File' class in
    >>>the global Object namespace with my own File class and then revert it
    >>>back to the standard when I'm done?
    >>>
    >>>rgds
    >>>Dema
    >>>http://dema.ruby.com.br

    >>
    >>My general response would be: don't do that. My personal experience is
    >>that slapping existing Ruby classes under your own namespace is a bad
    >>idea. It's just a pain. The fact that you're trying to use the "short
    >>form" only reinforces that it's unnecessary.
    >>
    >>Just put your methods directly in the File class without the added
    >>namespace.
    >>
    >>Regards,
    >>
    >>Dan

    >
    >
    >
    Gennady Bystritksy, Jul 12, 2005
    #4
    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. Ruslan

    namespace's conflict

    Ruslan, Jan 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,332
    netnews.microsoft.com
    Mar 1, 2005
  2. Andrew FPGA
    Replies:
    0
    Views:
    955
    Andrew FPGA
    Sep 26, 2005
  3. Joachim Spoerhase
    Replies:
    1
    Views:
    1,372
    Dimitre Novatchev
    Jul 16, 2003
  4. charles cashion

    css conflict (or html conflict)

    charles cashion, Feb 18, 2009, in forum: HTML
    Replies:
    2
    Views:
    775
    charles cashion
    Feb 18, 2009
  5. rocky
    Replies:
    2
    Views:
    310
    rocky
    Jul 7, 2009
Loading...

Share This Page