Ruby 相当于 Python 的“dir”?
在Python中,我们可以“dir”一个模块,如下所示:
>>> import re
>>> dir(re)
它列出了模块中的所有函数。 Ruby 中有类似的方法吗?
In Python we can "dir" a module, like this:
>>> import re
>>> dir(re)
And it lists all functions in the module. Is there a similar way to do this in Ruby?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
据我所知,不太清楚,但你会得到某个地方
As far as I know not exactly but you get somewhere with
我喜欢在我的 .irbrc 中包含这个:
所以当我在 irb 中时:
或者甚至更可爱 - 使用 grep:
I like to have this in my .irbrc:
So when I'm in irb:
Or even cuter - with grep:
在 irb 中“搜索”方法的提示:
在值上尝试方法进行比较的提示:
另外,请注意,您不会通过 object.methods 获得与 Python 目录相同的信息。 您必须使用 object.methods 和 class.constants 的组合,以及 class.singleton_methods 来获取类方法。
Tip for "searching" for a method in irb:
Tip for trying out methods on a value for comparison:
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.
methods
方法将列出可以在该对象上调用的所有方法。 它列出了对象的类定义的所有方法。还有其他类似的方法,例如
instance_methods
,您可以在文档中阅读:例如 https://ruby-doc.org/core-2.7.3/Module.html#instance_methods-methodThe
methods
method will list all the methods that can be called on the object. It lists all the methods that the object's class defines.There are other methods like this, such as
instance_methods
which you can read about in the docs: e.g https://ruby-doc.org/core-2.7.3/Module.html#instance_methods-method也许没有回答原始问题(取决于用例),但对于那些希望仅在 irb 中使用的人,您可以使用“double-TAB”进行自动补全。 实际上,它还可以列出(几乎所有)可用于给定对象的方法。
将以下行放入您的
~/.irbrc
文件中:现在,(重新)启动
irb
,开始键入方法并按 TAB 两次 - irb 自动完成输入!我实际上是在这里学到的: http://drnicwilliams.com/2006/ 10/12/my-irbrc-for-consoleirb/
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.Put the following line into your
~/.irbrc
file:Now, (re)start the
irb
, start typing a method and hit TAB twice - irb autocompletes the input!I actually learned it here: http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
给出方法排序数组的 yaml 表示形式。 请注意,这可用于列出类和对象的方法。
gives a yaml representation of the sorted array of methods. Note that this can be used to list the methods of both classes and objects.
并不真地。 就像其他人所说,您可以通过列出类实例方法(例如 String.instance_methods )来获得您想要的部分内容,但是如果您打开的文件重新打开一个类,这对您没有帮助(除非您检查之前和之后)。
如果您不需要以编程方式访问方法列表,请考虑使用 ri 命令行工具查看类、模块或方法的文档。
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.我本来可以将此作为对 jonelf 答案的评论,但显然我没有足够的代表。
some_object.methods.sort - Object.new.methods
这并不完全是您所要求的,正如其他人所说,但它为您提供了您想要的信息。
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.
如果我严格阅读你的问题,我必须这样回答:Ruby 中
require
指定的文件只是一个容器,不一定与类有任何关系。 内容可以是:或者以上的任意组合,多次。 所以你不能直接询问给定文件中的所有方法。
如果您打算列出给定模块或类的所有方法,那么其他答案就是您所寻求的(主要是在模块名称或类上使用
#methods
方法)。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: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).