Ruby 相当于 Python 的“dir”?

发布于 2024-07-13 03:12:46 字数 144 浏览 7 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

裸钻 2024-07-20 03:12:46

据我所知,不太清楚,但你会得到某个地方

object.methods.sort

As far as I know not exactly but you get somewhere with

object.methods.sort
寂寞笑我太脆弱 2024-07-20 03:12:46

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

I like to have this in my .irbrc:

class Object
  def local_methods
    (methods - Object.instance_methods).sort
  end
end

So when I'm in 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:

>> Time.now.local_methods.grep /str/
=> ["strftime"]
尽揽少女心 2024-07-20 03:12:46

在 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 来获取类方法。

Tip for "searching" for a method in 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.

睫毛溺水了 2024-07-20 03:12:46

methods 方法将列出可以在该对象上调用的所有方法。 它列出了对象的类定义的所有方法。

>> "a string".methods
=> [:unicode_normalize, :unicode_normalize!, :ascii_only?, :to_r, :encode, ... ]

还有其他类似的方法,例如 instance_methods,您可以在文档中阅读:例如 https://ruby-doc.org/core-2.7.3/Module.html#instance_methods-method

The 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.

>> "a string".methods
=> [:unicode_normalize, :unicode_normalize!, :ascii_only?, :to_r, :encode, ... ]

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

忆沫 2024-07-20 03:12:46

也许没有回答原始问题(取决于用例),但对于那些希望仅在 irb 中使用的人,您可以使用“double-TAB”进行自动补全。 实际上,它还可以列出(几乎所有)可用于给定对象的方法。

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

require 'irb/completion'

现在,(重新)启动 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:

require 'irb/completion'

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/

仙气飘飘 2024-07-20 03:12:46
y String.methods.sort

给出方法排序数组的 yaml 表示形式。 请注意,这可用于列出类和对象的方法。

y String.methods.sort

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.

凑诗 2024-07-20 03:12:46

并不真地。 就像其他人所说,您可以通过列出类实例方法(例如 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.

—━☆沉默づ 2024-07-20 03:12:46

我本来可以将此作为对 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.

羁拥 2024-07-20 03:12:46

如果我严格阅读你的问题,我必须这样回答: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:

  • 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文