Ruby的等效Python的“目录”?

在Python中,我们可以“模仿”一个模块,就像这样:

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

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

据我所知不完全是,但你得到的地方

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

您可以使用一个模块,例如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?"] 

提示“search”irb中的方法:

 "something".methods.select {|item| item =~ /query/ } 

提示尝试用于比较的方法:

 value = "something" [:upcase, :downcase, :capitalize].collect {|method| [method, value.send(method)] } 

另外,请注意,您将不会获得与Python的dir和object.methods相同的信息。 您必须使用object.methods和class.constants的组合,也可以使用class.singleton_methods来获取类方法。

我会去做这样的事情:

 y String.methods.sort 

这将给你一个sorting的数组方法的yaml表示。 请注意,这可以用来列出类和对象的方法。

不是真的。 就像其他人说的,通过列举类实例方法(例如String.instance_methods ),你可以得到你想要的一部分,但是如果你打开的文件重新打开一个类(除非你在前后检查),这并不能帮助你。

如果您不需要对方法列表进行编程访问,请考虑使用ri命令行工具检查类,模块或方法的文档。

我会对jonelf的回答发表评论,但显然我没有足够的代表。

some_object.methods.sort – Object.new.methods

这不像其他人所说的那样,但它给了你后面的信息。

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

  • 一类
  • 一个模块
  • 明码

或者上述的任何组合,几次。 所以你不能直接要求给定文件中的所有方法。

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

也许不回答原来的问题(取决于用例),但是对于那些只在irb使用的用户,可以使用“double-tab”来自动完成。 其中,有效地,还可以列出(几乎所有)给定对象的可用方法。

把下面一行放到你的~/.irbrc文件中:

 require 'irb/completion' 

现在,(重新)启动irb ,开始input一个方法,然后按TAB两次 – irb autocompletesinput!

我在这里学到了东西: http : //drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/