Ruby版的Python“dir”?

时间:2022-09-01 22:47:40

In Python we can "dir" a module, like this:

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

>>> import re
>>> dir(re)

And it lists all functions in the module. Is there a similar way to do this in Ruby?

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

9 个解决方案

#1


47  

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

据我所知不完全是这样,但你还是有办法的

object.methods.sort

#2


18  

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:

当我在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"]

#3


4  

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?"]

#4


4  

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的dir相同的信息。你必须使用对象的组合。方法和类。常数,也类。获得类方法的singleton_methods。

#5


1  

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表示。注意,这可以用来列出类和对象的方法。

#6


0  

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命令行工具检查类、模块或方法的文档。

#7


0  

I would have made this a comment to jonelf's answer, but apparently I don't have enough rep.

我本想对jonelf的回答做个评论,但显然我没有足够的代表。

some_object.methods.sort - Object.new.methods

some_object.methods。排序——Object.new.methods

This isn't exactly what you were asking as others have said, but it gives you the info you are after.

这并不是你想要的,就像其他人说的那样,但是它会告诉你你想要的信息。

#8


0  

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:

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

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

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

#9


0  

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/。

#1


47  

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

据我所知不完全是这样,但你还是有办法的

object.methods.sort

#2


18  

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:

当我在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"]

#3


4  

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?"]

#4


4  

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的dir相同的信息。你必须使用对象的组合。方法和类。常数,也类。获得类方法的singleton_methods。

#5


1  

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表示。注意,这可以用来列出类和对象的方法。

#6


0  

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命令行工具检查类、模块或方法的文档。

#7


0  

I would have made this a comment to jonelf's answer, but apparently I don't have enough rep.

我本想对jonelf的回答做个评论,但显然我没有足够的代表。

some_object.methods.sort - Object.new.methods

some_object.methods。排序——Object.new.methods

This isn't exactly what you were asking as others have said, but it gives you the info you are after.

这并不是你想要的,就像其他人说的那样,但是它会告诉你你想要的信息。

#8


0  

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:

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

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

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

#9


0  

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/。