How to distinguish class "ClassA" from "Module::ClassA"?
So my problem is that I have two classes with the same name.
One is R开发者_如何学PythonEXML::Document, which is a third party library.
the other is an ActiveRecord model called "Document" controlled by my application.
In my controller when I do "Document.transaction" he tells me that method "transaction" doesn't exist for class "REXML::Document" but what I really want is to use my model. My model doesn't belong to a module so how can I tell ruby that he should look to the model "Document" instead of "REXML::Document" ?
::Document
tells Ruby to start the name lookup at the top-level as opposed to the current level.
It's similar a Unix pathname, where the /
also functions both as a pathname component separator and as a flag to not use relative lookup.
However, if you are inside your controller, Document
by itself should never refer to REXML::Document
. It should always only look for Document
, first in the current namespace, then one level up, and so forth until the top-level namespace (which is actually nothing special, it's just the Object
class).
Unless, of course, you have include REXML
somewhere in your code, in which case, there is an even simpler remedy than using ::Document
: just don't do that.
Yes, plenty of duplicated of this question I think.
The answer is, in any case, to use ::Document
although I would have thought just typing Document
would have preferred Document
to REXML::Document
精彩评论