Python的Ruby等价物';s";目录;?

Python的Ruby等价物';s";目录;?,python,ruby,inspection,Python,Ruby,Inspection,在Python中,我们可以“dir”一个模块,如下所示: >>> import re >>> dir(re) y String.methods.sort 它列出了模块中的所有功能。在Ruby中有类似的方法吗?据我所知,不太准确,但你可以通过 object.methods.sort 您可以选择一个模块,例如可枚举,然后发送方法方法,该方法列出了模块定义的所有方法。包含此模块的类将响应这些方法 >> Enumerable.methods =>

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

>>> import re
>>> dir(re)
y String.methods.sort

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

据我所知,不太准确,但你可以通过

object.methods.sort

您可以选择一个模块,例如
可枚举
,然后发送
方法
方法,该方法列出了模块定义的所有方法。包含此模块的类将响应这些方法

>> 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?"]
>Enumerable.methods

=>[“检查”、“私有类方法”、“常量缺失”、“克隆”、“方法”、“公共类方法”、“公共实例方法”、“实例变量定义?”、“方法定义?”、“相等?”、“冻结”、“包含的模块”、“常量获取”、“yaml-as”、“方法”、“响应?”、“模块评估”、“类变量”、“dup”、“受保护的实例方法”、“实例变量”“公共方法已定义?”、“公共方法已定义?”、“单一方法”、“发送”、“类评估”、“污染”、“冻结?”、“实例变量获取”、“包括?”、“私有实例方法”、“私有实例方法”、“发送”、“实例的实例”、“私有方法已定义?”、“到名称”、“到yaml样式”,“自动加载”、“类型”、“yaml\u标记\类\名称”、“显示”、“实例\方法”、“实例\变量\集”、“到\ yaml\属性”、“种类?”、“扩展”、“受保护\方法\定义的?”、“常量\定义的?”、“>=”、“祖先”、“到\ s”、“我会这样做:

>>> import re
>>> dir(re)
y String.methods.sort
这将为您提供方法排序数组的yaml表示。请注意,这可用于列出类和对象的方法。

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

"something".methods.select {|item| item =~ /query/ }
在值上尝试方法以进行比较的提示:

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

另外,请注意,使用object.methods无法获得与Python的dir相同的所有信息。您必须结合使用object.methods和class.constants,以及class.singleton_方法来获得类方法。

不太可能。正如其他人所说,您可以通过列出类实例方法来获得部分所需信息(例如,
String.instance\u方法
),但如果打开的文件重新打开了类,则这对您没有帮助(除非您在打开之前和之后进行了检查)


如果您不需要编程访问方法列表,请考虑使用<代码> RI 命令行工具检查类、模块或方法的文档。

< P>我会对JoNeLef的回答作出评论,但显然我没有足够的R.P.P/>。 一些\u object.methods.sort-object.new.methods


这并不是像其他人所说的那样你所问的问题,但它提供了你想要的信息。

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

  • 班级
  • 模块
  • 明码
或者以上的任意组合,多次。因此,您不能直接请求给定文件中的所有方法


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

我希望在我的。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"]

可能没有回答最初的问题(取决于用例),但是对于那些只希望在
irb
中使用这个问题的人来说,你可以使用“双标签”进行自动完成。实际上,它还可以列出(几乎所有)可用于给定对象的方法

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

require 'irb/completion'
现在,(重新)启动
irb
,开始键入一个方法并点击TAB两次-irb自动完成输入


实际上我是在这里学到的:

非常感谢,我从Python学习Ruby,这是一个很大的帮助。只是想知道你的方法名,“局部”方法是什么?既然全局方法是静态方法,我猜局部方法是实例方法?dir()还包括在该范围内设置的变量,而不仅仅是定义的函数。另请参见:这是错误的。这并没有“列出模块定义的所有方法”。这列出了可以在
可枚举
上调用的所有方法,下面列出了
可枚举
类的所有方法(即
模块
)定义。