Ruby 相当于 Python 的“目录”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/468421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby equivalent of Python's "dir"?
提问by Jonas Elfstr?m
In Python we can "dir" a module, like this:
在 Python 中,我们可以“定向”一个模块,如下所示:
>>> import re
>>> dir(re)
And it lists all functions in the module. Is there a similar way to do this in Ruby?
它列出了模块中的所有功能。在 Ruby 中是否有类似的方法可以做到这一点?
回答by Jonas Elfstr?m
As far as I know not exactly but you get somewhere with
据我所知,不完全是,但你到了某个地方
object.methods.sort
回答by Commander Keen
I like to have this in my .irbrc:
我喜欢在我的 .irbrc 中有这个:
class Object
def local_methods
(methods - Object.instance_methods).sort
end
end
So when I'm in irb:
所以当我在 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"]
Or even cuter - with grep:
甚至更可爱 - 使用 grep:
>> Time.now.local_methods.grep /str/
=> ["strftime"]
回答by method
Tip for "searching" for a method in irb:
在 irb 中“搜索”方法的提示:
"something".methods.select {|item| item =~ /query/ }
Tip for trying out methods on a value for comparison:
尝试比较值的方法的提示:
value = "something"
[:upcase, :downcase, :capitalize].collect {|method| [method, value.send(method)] }
Also, note that you won't get all the same information as Python's dir with object.methods. You have to use a combination of object.methods and class.constants, also class.singleton_methods to get the class methods.
另外,请注意,使用 object.methods 不会获得与 Python 目录相同的所有信息。您必须使用 object.methods 和 class.constants 的组合,还有 class.singleton_methods 来获取类方法。
回答by dylanfm
You can take a module, such as Enumerable
, and send the methods
method which lists all the methods the module defines. Classes that include this module will respond to these 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?"]
回答by Codebeef
I'd go for something like this:
我会去做这样的事情:
y String.methods.sort
Which will give you a yaml representation of the sorted array of methods. Note that this can be used to list the methods of both classes and objects.
这将为您提供排序的方法数组的 yaml 表示。请注意,这可用于列出类和对象的方法。
回答by Timur
Maybe not answering the original question (depends on the use case), but for those who are looking for this to be used in the irb
only, you can use "double-TAB" for autocompletion. Which, effectively, can also list (almost all) the methods available for a given object.
也许没有回答最初的问题(取决于用例),但是对于那些正在寻找irb
仅用于此的人,您可以使用“double-TAB”进行自动完成。这实际上还可以列出(几乎所有)可用于给定对象的方法。
Put the following line into your ~/.irbrc
file:
将以下行放入您的~/.irbrc
文件中:
require 'irb/completion'
Now, (re)start the irb
, start typing a method and hit TAB twice - irb autocompletes the input!
现在,(重新)启动irb
,开始输入一个方法并按两次 TAB - irb 自动完成输入!
I actually learned it here: http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
我实际上是在这里学到的:http: //drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
回答by rampion
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).
并不真地。就像其他人所说的那样,您可以通过列出类实例方法(例如String.instance_methods
)来获得您想要的部分内容,但是如果您打开的文件重新打开一个类(除非您前后检查),这对您没有帮助。
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.
如果您不需要以编程方式访问方法列表,请考虑使用ri
命令行工具查看类、模块或方法的文档。
回答by jshen
I would have made this a comment to jonelf's answer, but apparently I don't have enough rep.
我本可以对 jonef 的回答发表评论,但显然我没有足够的代表。
some_object.methods.sort - Object.new.methods
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.
这与其他人所说的不完全相同,但它为您提供了所需的信息。
回答by Keltia
If I stricly read your question, I must answer it that way: a file as specified by require
in Ruby is just a container and does not have necessarely have any relation with a class. The content can be:
如果我严格阅读您的问题,我必须这样回答:require
Ruby 中指定的文件只是一个容器,不一定与类有任何关系。内容可以是:
- a class
- a module
- plain code
- 一类
- 一个模块
- 明码
or any combination of the above, several times. So you can not directly ask for all methods in a given file.
或以上的任何组合,多次。所以你不能直接要求给定文件中的所有方法。
If you meant to list all methods of a given module or class, then the other answers are what you seek (mainly using the #methods
method on a module name or class).
如果您打算列出给定模块或类的所有方法,那么其他答案就是您所寻求的(主要使用#methods
模块名称或类上的方法)。