views:

24

answers:

3

I have two Const with the same name; One is a global const, and the other is defined under the namespace Admin. 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!

A: 

using it's #class or #to_s or #inspect may help you know if your defined object is actually global or not.

rogerdpack
A: 

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

Marc-André Lafortune
Exactly what I want. Thanks Lafortune!
Croplio
+1  A: 

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"
Paul Rubel