如何通过reflection获取由Ruby的Module类定义的常量?
我试图让Matz和Flanagan的“Ruby编程语言”元编程章节进入我的脑海,但是我无法理解我梦寐以求的代码片段的输出:
p Module.constants.length # => 88 $snapshot1 = Module.constants class A NAME=:abc $snapshot2 = Module.constants p $snapshot2.length # => 90 p $snapshot2 - $snapshot1 # => ["A", "NAME"] end p Module.constants.length # => 89 p Module.constants - $snapshot1 # => ["A"] p A.constants # => ["NAME"]  本书指出类方法constants返回类的constants列表(正如你可以在A.constants的输出中看到的A.constants )。 当我遇到上述奇怪的行为时,我试图获取为Module类定义的常量列表。 
  A的常量显示在Module.constants中。 如何获得Module类定义的常量列表? 
文档状态
Module.constants返回系统中定义的所有常量。 包括所有类和方法的名称
 由于A从Module.constantsinheritance它的实现,它在基类和派生types中的行为有什么不同? 
 p A.class # => Class p A.class.ancestors # => [Class, Module, Object, Kernel] 
  注意:如果您使用的是Ruby 1.9, constants将返回一个符号数组而不是string。 
好问题!
 你的困惑是由于类方法Module.constants隐藏了Module#constants的实例方法Module#constants 。 
在Ruby 1.9中,通过添加可选参数解决了这个问题:
 # No argument: same class method as in 1.8: Module.constants # ==> All constants # One argument: uses the instance method: Module.constants(true) # ==> Constants of Module (and included modules) Module.constants(false) # ==> Constants of Module (only). 
 在上面的例子中, A.constants调用Module#constants (实例方法),而Module.constants调用Module.constants 。 
 在Ruby 1.9中,您因此需要调用Module.constants(true) 。 
 在Ruby 1.8中,可以调用Module上的实例方法#constants 。 您需要获取实例方法并将其绑定为一个类方法(使用不同的名称): 
 class << Module define_method :constants_of_module, Module.instance_method(:constants) end # Now use this new class method: class Module COOL = 42 end Module.constants.include?("COOL") # ==> false, as you mention Module.constants_of_module # ==> ["COOL"], the result you want 
 我希望我能够将1.9的function完全恢复到1.8的backports gem,但我不能想到一个方法来获得一个模块的常量,不包括inheritance的,在Ruby 1.8中。 
编辑 :只是改变了官方文件,以正确反映这…
马克回答之后,我不得不回到我的思想洞穴。 更多的代码片段,然后更多。 最后,当Ruby的方法parsing看起来有意义的时候,把它写成博客文章,这样我就不会忘记。
符号:如果A是A的本征类
 当A.constants被调用,方法parsing(参考我的博客文章中的图像有一个视觉帮助)按顺序查找以下位置 
-   MyClass",Object",BasicObject"(单例方法)
-   Class(实例方法)
-   Module(实例方法)
-   Object(实例方法)和内核
-   BasicObject(实例方法)
  Rubyfind实例方法Module#constants 
 当Module.constants ,Ruby会查看 
-   Module",”Object",Module"Object"BasicObject"(单例方法)
-   Class(实例方法)
-   Module(实例方法)
-   Object(实例方法)和内核
-   BasicObject(实例方法)
 这一次,Ruby在Module".constantsfind了singleton / class方法,正如Marc所说的那样。 
Module定义了一个影响实例方法的单例方法。 单例方法返回所有已知的常量,而实例方法返回当前类及其祖先中定义的常量。