如何获取类方法?

Tie*_*Dad 8 ruby

RubyModule有一个方法instance_methods可以返回该类的方法列表。但是如何检索类的类方法列表呢?

use*_*869 8

您可以将false参数传递给Object#methods

class Foo
  def self.bar
  end
end

Foo.methods(false) #=> [:bar]
Run Code Online (Sandbox Code Playgroud)


unc*_*one 0

class Song\n  def self.hello;end\nend\np Song.singleton_methods\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果\xef\xbc\x9a

\n\n
[:hello]\n
Run Code Online (Sandbox Code Playgroud)\n