Ruby: 如何找到模块的所有 class 方法
Ruby: how can I find all class method of module
例如我有这个代码:
module ExampleModule
def self.module_method
end
def normal_method
end
end
如果我尝试调用 ExampleModule.instance_methods
,我只能看到 normal_method
。我也在 singleton_class
中搜索过,但看起来 Ruby 没有将 class 方法放入 singleton class
:
ExampleModule.singleton_class.each do |method|
print method
end
怎么可以看到self.module_method
(而且只有这个方法,没有模块ExampleModule的其他父方法)。
谢谢
ExampleModule.methods(false)
#=> [:module_method]
ExampleModule.singleton_class.instance_methods(false)
#=> [:module_method]
ExampleModule.instance_methods(false)
#=> [:normal_method]
例如我有这个代码:
module ExampleModule
def self.module_method
end
def normal_method
end
end
如果我尝试调用 ExampleModule.instance_methods
,我只能看到 normal_method
。我也在 singleton_class
中搜索过,但看起来 Ruby 没有将 class 方法放入 singleton class
:
ExampleModule.singleton_class.each do |method|
print method
end
怎么可以看到self.module_method
(而且只有这个方法,没有模块ExampleModule的其他父方法)。
谢谢
ExampleModule.methods(false)
#=> [:module_method]
ExampleModule.singleton_class.instance_methods(false)
#=> [:module_method]
ExampleModule.instance_methods(false)
#=> [:normal_method]