Ruby 为什么';t模块。定义的方法?(:方法)是否正常工作?

Ruby 为什么';t模块。定义的方法?(:方法)是否正常工作?,ruby,Ruby,我试图使用module.method\u defined?(:method)检查模块中是否定义了一个方法,它返回false,应该返回true module Something def self.another 1 end end Something.methods列出了'other',但是Something.method\u定义了(:other)返回false 因为方法是在self上定义的,所以这可能不起作用吗?如果是这种情况,除了使用方法\u定义??来检查模块上是否定义了方法

我试图使用
module.method\u defined?(:method)
检查模块中是否定义了一个方法,它返回false,应该返回true

module Something
  def self.another
    1
  end
end
Something.methods
列出了'other',但是
Something.method\u定义了(:other)
返回
false


因为方法是在self上定义的,所以这可能不起作用吗?如果是这种情况,除了使用
方法\u定义?

来检查模块上是否定义了方法之外,是否还有其他方法来检查该方法。要知道模块是否有模块方法,可以使用respond\u to? 上 模块:


方法定义?将告诉您包含模块的类的实例是否响应给定的方法。

模块方法在其元类中定义。因此,您还可以通过以下方式检查方法是否包含:

k = class << Something; self; end # Retrieves the metaclass
k.method_defined?(:another)  #=> true
k=class true

您可以在中阅读更多信息。

我正在添加我的答案版本

使用singleton_方法:

module Something
  def self.another
  end
end

Something.singleton_methods.include?(:another) #=> true, with all parent modules
Something.singleton_methods(false).include?(:another) #=> true, will check only in the Something module

至少可以说,该网站上的图表令人困惑。实例“继承”类中的方法是什么意思?对我来说似乎是个错误的术语。另外,指向元类的标有
instance\u eval
的箭头是什么意思
instance_eval
求值不会发生在元类上,而是发生在实例上-唯一的例外是
instance_eval
中的
def
行为,它定义了元类上的方法。谢谢!不知道为什么另一个答案会打勾。这就是实际操作的方法。也许我读错了,但不应该是
什么。回答:(:另一个)
所以
另一个
是一个符号。否则,
另一个
不会导致未定义的错误吗?
module Something
  def self.another
  end
end

Something.singleton_methods.include?(:another) #=> true, with all parent modules
Something.singleton_methods(false).include?(:another) #=> true, will check only in the Something module