2017-02-04 38 views
-1

例如,我有这样的代码:红宝石:我怎样才能找到模块的所有类方法

module ExampleModule 
    def self.module_method 
    end 

    def normal_method 
    end 
end 

如果我尝试致电ExampleModule.instance_methods,我只能看到normal_method。找遍了也singleton_class但看起来像Ruby没有把类方法中singleton class

ExampleModule.singleton_class.each do |method| 
    print method 
end 

怎么能看到self.module_method(只有这个方法,模块ExampleModule的不是其他父类的方法)。

感谢

+0

'ExampleModule.methods' – trueinViso

+0

我不会称之为 “元编程”。这只是使用普通的'send'。 –

+0

@trueinViso但它会显示所有的方法。我只想在这个模块中只显示自定义的方法。 –

回答

5
ExampleModule.methods(false) 
    #=> [:module_method] 
ExampleModule.singleton_class.instance_methods(false) 
    #=> [:module_method] 
ExampleModule.instance_methods(false) 
    #=> [:normal_method]