开发者

How to judge whether a Constant has been defined within a module namespace, not a global one?

开发者 https://www.devze.com 2023-01-10 14:25 出处:网络
I have two Const with the same name; One is a global const, and the other is defined under the namespace A开发者_StackOverflow中文版dmin. But I need to distinguish them;The global one has already defi

I have two Const with the same name; One is a global const, and the other is defined under the namespace A开发者_StackOverflow中文版dmin. But I need to distinguish them;The global one has already defined, and the scoped one need to auto defined if it has not been defined yet:


A = 'A Global Const'  
module Admin  
  A = 'A Const within the Admin namespace' if const_defined? 'A'  # always true and the Admin::A can never be defined!
end  
puts A  # => 'A Global Const' 
puts Admin::A  # => NameError: uninitialized constant Admin::A
# the Admin::A will never be defined.

But if the Global A is defined, the "const_defind?" part will always return ture!

I even have tried:


... if defined? A  
... if self.const_defined? 'A'  
... if Object.const_get('Admin').const_defined? 'A'  

Always true!

I need to distinguish them because I need to use the A in A and Admin::A two forms;

Like the situation PostsController for public use, and Admin::PostsController for admin use;

Help!


You should try scoping both of them to test just the one you want

module Adm
  A = "FOO"
end
defined?(A) # -> nil
defined?(Adm::A) # "constant"
defined?(::A) # -> nil
A = "BAR"
defined?(::A) # -> "constant
::A # => "BAR"
Adm::A # => "FOO"


Indeed, const_defined? and const_get go through the hierarchy, which for modules include (artificially) the Object class. You can use Module#constants to avoid this, though:

module Admin  
  A = 'A Const with in the Admin namespace' unless constants.include?(:A)
end

Note: In Ruby 1.8, you check against "A", not :A


Using its #class or #to_s or #inspect may help you know if your defined object is actually global or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消