58

在 Python 中,我们可以“dir”一个模块,如下所示:

>>> import re
>>> dir(re)

它列出了模块中的所有功能。在 Ruby 中是否有类似的方法可以做到这一点?

4

9 回答 9

62

据我不完全知道,但你得到某个地方

object.methods.sort
于 2009-01-22T08:39:41.987 回答
22

我喜欢在我的 .irbrc 中有这个:

class Object
  def local_methods
    (methods - Object.instance_methods).sort
  end
end

所以当我在 irb 时:

>> Time.now.local_methods 
=> ["+", "-", "<", "<=", "<=>", ">", ">=", "_dump", "asctime", "between?", "ctime", "day", "dst?", "getgm", "getlocal", "getutc", "gmt?", "gmt_offset", "gmtime", "gmtoff", "hour", "isdst", "localtime", "mday", "min", "mon", "month", "sec", "strftime", "succ", "to_f", "to_i", "tv_sec", "tv_usec", "usec", "utc", "utc?", "utc_offset", "wday", "yday", "year", "zone"]

甚至更可爱 - 使用 grep:

>> Time.now.local_methods.grep /str/
=> ["strftime"]
于 2009-04-01T08:20:38.150 回答
5

在 irb 中“搜索”方法的提示:

"something".methods.select {|item| item =~ /query/ }

尝试比较值的方法的提示:

value = "something"
[:upcase, :downcase, :capitalize].collect {|method| [method, value.send(method)] }

另外,请注意,您不会通过 object.methods 获得与 Python 目录相同的所有信息。您必须使用 object.methods 和 class.constants 的组合,以及 class.singleton_methods 来获取类方法。

于 2009-01-22T11:33:44.913 回答
3

您可以获取一个模块,例如Enumerable,并发送methods列出该模块定义的所有方法的方法。包含此模块的类将响应这些方法。

>> Enumerable.methods
=> ["inspect", "private_class_method", "const_missing", "clone", "method", "public_methods", "public_instance_methods", "instance_variable_defined?", "method_defined?", "equal?", "freeze", "included_modules", "const_get", "yaml_as", "methods", "respond_to?", "module_eval", "class_variables", "dup", "protected_instance_methods", "instance_variables", "public_method_defined?", "__id__", "object_id", "taguri", "yaml_tag_read_class", "eql?", "const_set", "id", "to_yaml", "taguri=", "singleton_methods", "send", "class_eval", "taint", "frozen?", "instance_variable_get", "include?", "private_instance_methods", "__send__", "instance_of?", "private_method_defined?", "to_a", "name", "to_yaml_style", "autoload", "type", "yaml_tag_class_name", "<", "protected_methods", "instance_eval", "<=>", "==", ">", "display", "===", "instance_method", "instance_variable_set", "to_yaml_properties", "kind_of?", "extend", "protected_method_defined?", "const_defined?", ">=", "ancestors", "to_s", "<=", "public_class_method", "hash", "class", "instance_methods", "tainted?", "=~", "private_methods", "class_variable_defined?", "nil?", "untaint", "constants", "autoload?", "is_a?"]
于 2009-01-22T08:41:01.267 回答
1

我会去做这样的事情:

y String.methods.sort

这将为您提供已排序方法数组的 yaml 表示形式。请注意,这可用于列出类和对象的方法。

于 2009-01-22T10:25:56.780 回答
1

也许没有回答原始问题(取决于用例),但对于那些正在寻找irb仅用于此的人,您可以使用“双制表符”进行自动完成。实际上,它还可以列出(几乎所有)给定对象可用的方法。

将以下行放入您的~/.irbrc文件中:

require 'irb/completion'

现在,(重新)启动irb,开始输入一个方法并按两次 TAB - irb 自动完成输入!

我实际上是在这里学到的:http: //drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/

于 2014-06-02T19:32:19.757 回答
0

I would have made this a comment to jonelf's answer, but apparently I don't have enough rep.

some_object.methods.sort - Object.new.methods

This isn't exactly what you were asking as others have said, but it gives you the info you are after.

于 2009-01-22T16:53:00.120 回答
0

Not really. Like the others said, you can get part of what you want by listing class instance methods (e.g. String.instance_methods) but that doesn't help you if a file you open reopens a class (unless you check before and after).

If you don't need programmatic access to the list of methods, consider checking out the documentation for a class, module or method using the ri command line tool.

于 2009-01-22T12:57:58.750 回答
0

如果我严格阅读您的问题,我必须这样回答:requireRuby 中指定的文件只是一个容器,与类没有任何关系。内容可以是:

  • 一类
  • 一个模块
  • 明码

或以上任意组合,多次。所以你不能直接询问给定文件中的所有方法。

如果您打算列出给定模块或类的所有方法,那么其他答案就是您所寻求的(主要使用#methods模块名称或类上的方法)。

于 2009-01-22T21:50:35.137 回答