ruby - How to judge whether a Constant has been defined within a module namespace, not a global one? -
I have two const with the same name. One is a global constellation, and the other is defined under the namespace admin. But I need to distinguish them; The global one has already defined, and Scod needs to define itself, if it has not yet been defined:
A = 'A global cost 'Module admin A =' a containin within the admin namespace 'if const_defined? 'A' # Always true and admin :: A can never be defined! Finally A # = & gt; The 'A Global Cost' administrator puts: A # = & gt; Name: Error: Unused continuous administrator :: A # Administrator :: A will never be defined
But if global A is defined, "const_defind?" The part will always be right!
I have also tried:
... if defined? A ... if self.const_defined? 'A' ... if Object.const_get ('Admin'). Const_defined? 'A'
Always the truth!
I need to separate them because I have to use A and Admin :: A form; Status Post Controller for Public Use, and Administrator: Post Controller for Administrator Use; Help!
In fact, const_defined?
for the module. Code>, however: module # constant
and const_get
go through the hierarchy, which contains (code) the
module admin A = 'An arc with admin namespace' unless it is a constant? (: A) End
Note : In Ruby 1.8, you check against "A"
, no : A
Comments
Post a Comment