在Ruby中,我怎样才能找到方法所在的类?

在Ruby中,我怎样才能找到方法所在的类?,ruby,Ruby,我怎样才能知道在Ruby中定义了一个方法的类 例如,假设我想知道哪些类实现了to\s方法。如何使用ri命令进行编辑?编辑|由于您编辑了您的问题,此答案毫无意义;)断章取义 ~$ ri Enter the method name you want to look up. You can use tab to autocomplete. Enter a blank line to exit. >> to_s = .to_s (from gem actionpack-3.1.0.r

我怎样才能知道在Ruby中定义了一个方法的类


例如,假设我想知道哪些类实现了
to\s
方法。如何使用
ri
命令进行编辑?

编辑|由于您编辑了您的问题,此答案毫无意义;)断章取义

~$ ri

Enter the method name you want to look up.
You can use tab to autocomplete.
Enter a blank line to exit.

>> to_s

= .to_s

(from gem actionpack-3.1.0.rc6)
=== Implementation from ActionDispatch::RemoteIp::RemoteIpGetter
------------------------------------------------------------------------------
  to_s()

------------------------------------------------------------------------------


(from gem actionpack-3.1.0.rc6)
=== Implementation from ActionView::FileSystemResolver
------------------------------------------------------------------------------
  to_s()

------------------------------------------------------------------------------


(from gem actionpack-3.1.0.rc6)
=== Implementation from ActionView::FixtureResolver
------------------------------------------------------------------------------
  to_s()
非常有效的方法。我个人不明白你为什么需要这个:

class ClassEnumerator
  def each(&block)
    ObjectSpace.each_object(Class, &block)
  end

  include Enumerable
end

ClassEnumerator.new.select { |klass| klass.instance_methods.include?(:merge) }
这应该可以找到所有实现
#merge
的类

pry(main)> ClassEnumerator.new.select { |klass| klass.instance_methods.include?(:merge) }
=> [OptionParser::CompletingHash,
 OptionParser::OptionMap,
 Hash,
 Gem::Dependency,
 Psych::Omap,
 Psych::Set,
 URI::MailTo,
 URI::LDAPS,
 URI::LDAP,
 CodeRay::CaseIgnoringWordList,
 CodeRay::WordList,
 URI::HTTPS,
 URI::HTTP,
 URI::FTP,
 URI::Generic]
pry(main)> 

您也可以键入
ri方法\u NAME
参见
man ri

您能举个例子说明您想做什么吗?