Ruby相当于Python的“dir”?

时间:2009-01-22 08:32:06

标签: python ruby inspection

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

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

它列出了模块中的所有功能。在Ruby中有类似的方法吗?

9 个答案:

答案 0 :(得分:52)

据我所知不完全,但你到了

object.methods.sort

答案 1 :(得分:20)

我喜欢在我的.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"]

答案 2 :(得分:4)

您可以使用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?"]

答案 3 :(得分:4)

提示“搜索”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来获取类方法。

答案 4 :(得分:1)

我会选择这样的事情:

y String.methods.sort

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

答案 5 :(得分:1)

可能没有回答原始问题(取决于用例),但对于那些只在irb中使用的人,可以使用“double-TAB”进行自动完成。实际上,这也可以列出(几乎所有)给定对象可用的方法。

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

require 'irb/completion'

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

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

答案 6 :(得分:0)

不是真的。就像其他人说的那样,你可以通过列出类实例方法(例如String.instance_methods)来获得你想要的一部分,但如果你打开的文件重新打开一个类(除非你之前和之后检查),这对你没有帮助。

如果您不需要以编程方式访问方法列表,请考虑使用ri命令行工具检查类,模块或方法的文档。

答案 7 :(得分:0)

我会对jonelf的回答发表评论,但显然我没有足够的代表。

some_object.methods.sort - Object.new.methods

这并不像其他人所说的那样,但它会为您提供您所追求的信息。

答案 8 :(得分:0)

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

  • 一堂课
  • 模块
  • 普通代码

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

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

相关问题