【发布时间】:2009-01-22 08:32:05
【问题描述】:
在 Python 中,我们可以“dir”一个模块,如下所示:
>>> import re
>>> dir(re)
它列出了模块中的所有功能。在 Ruby 中是否有类似的方法可以做到这一点?
【问题讨论】:
-
dir() 还包括已在该范围内设置的变量 - 不仅仅是定义的函数。
标签: python ruby inspection
在 Python 中,我们可以“dir”一个模块,如下所示:
>>> import re
>>> dir(re)
它列出了模块中的所有功能。在 Ruby 中是否有类似的方法可以做到这一点?
【问题讨论】:
标签: python ruby inspection
据我所知,不完全是,但你得到了某个地方
object.methods.sort
【讨论】:
我喜欢在我的 .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"]
【讨论】:
在 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 来获取类方法。
【讨论】:
您可以获取一个模块,例如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?"]
【讨论】:
Enumerable 上调用 的所有方法,IOW 它列出了Enumerable 的类(即Module)定义的所有方法。
我会选择这样的:
y String.methods.sort
这将为您提供已排序方法数组的 yaml 表示形式。请注意,这可用于列出类和对象的方法。
【讨论】:
可能没有回答原始问题(取决于用例),但对于那些希望仅在 irb 中使用此问题的人,您可以使用“双制表符”进行自动完成。实际上,它还可以列出(几乎所有)给定对象可用的方法。
将以下行放入您的 ~/.irbrc 文件中:
require 'irb/completion'
现在,(重新)启动irb,开始输入方法并按两次TAB - irb 自动完成输入!
其实我是在这里学的:http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
【讨论】:
不是真的。就像其他人说的那样,您可以通过列出类实例方法(例如String.instance_methods)来获得您想要的部分内容,但是如果您打开的文件重新打开了一个类(除非您在之前和之后检查),这对您没有帮助。
如果您不需要以编程方式访问方法列表,请考虑使用ri 命令行工具查看类、模块或方法的文档。
【讨论】:
我会对 jonelf 的回答发表评论,但显然我没有足够的代表。
some_object.methods.sort - Object.new.methods
这与其他人所说的并不完全一样,但它为您提供了您所追求的信息。
【讨论】:
如果我仔细阅读您的问题,我必须这样回答:Ruby 中require 指定的文件只是一个容器,与类没有必然关系。内容可以是:
或以上的任意组合,多次。所以你不能直接询问给定文件中的所有方法。
如果您打算列出给定模块或类的所有方法,那么其他答案就是您所寻求的(主要在模块名称或类上使用 #methods 方法)。
【讨论】: