ruby – 为什么Module.method_defined?(:方法)不能正常工作?
发布时间:2020-12-16 19:16:59 所属栏目:百科 来源:网络整理
导读:我正在尝试检查是否在使用Module.method_defined?(:方法)的模块中定义了一个方法,并且它返回false,它应该返回true. module Something def self.another 1 endend Something.methods列出了’another’但Something.method_defined?(:another)返回false. 这
我正在尝试检查是否在使用Module.method_defined?(:方法)的模块中定义了一个方法,并且它返回false,它应该返回true.
module Something def self.another 1 end end Something.methods列出了’another’但Something.method_defined?(:another)返回false. 这可能不起作用,因为该方法是在自我定义的吗?如果是这种情况,还有另一种方法可以检查方法是否在模块上定义,而不是使用method_defined? 解决方法
要知道模块是否有模块方法,您可以使用respond_to?
在…上 模块: Something.respond_to?(another) => true method_defined?将告诉您包含模块的类的INSTANCES是否响应给定的方法. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |