如何判断Constant是否已定义在模块命名空间内,而不是全局的?

发布于 2024-09-12 07:14:06 字数 756 浏览 4 评论 0原文

我有两个同名的常量;一个是全局常量,另一个是在命名空间 Admin 下定义的。但我需要区分它们;全局的已经定义了,作用域的如果尚未定义则需要自动定义:


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.

但是如果定义了全局A,则“const_defind?”部分将始终返回 ture!
我什至尝试过:


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

永远正确!
我需要区分它们,因为我需要使用 A 和 Admin::A 两种形式中的 A;
就像公共使用的 PostsController 和管理员使用的 Admin::PostsController 的情况;
帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演出会有结束 2024-09-19 07:14:06

您应该尝试确定两者的范围以仅测试您想要的一个

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"

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"
傲鸠 2024-09-19 07:14:06

事实上,const_define?const_get遍历层次结构,对于模块来说包括(人为Object类。不过,您可以使用 Module#constants 来避免这种情况:

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

注意:在 Ruby 1.8 中,您检查的是 "A",而不是 :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

对风讲故事 2024-09-19 07:14:06

使用其#class#to_s#inspect可以帮助您了解您定义的对象是否实际上是全局的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文