Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Namespace conflict resolution
Dema
7/12/2005 7:52:00 PM
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.r...
3 Answers
Berger, Daniel
7/12/2005 8:01:00 PM
0
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.r...
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
7/12/2005 9:10:00 PM
0
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.r...
>
> 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
7/12/2005 10:55:00 PM
0
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.r...
>>
>>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
>
>
>
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Namespace conflict resolution
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password