Ruby equivalent of Python's “dir”?

后端 未结 9 1556
温柔的废话
温柔的废话 2020-12-23 16:09

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

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

And it lists all functions in the module. Is ther

相关标签:
9条回答
  • 2020-12-23 16:37

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

    object.methods.sort
    
    0 讨论(0)
  • 2020-12-23 16:41

    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
    => ["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?"]
    
    0 讨论(0)
  • 2020-12-23 16:43

    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.

    0 讨论(0)
  • 2020-12-23 16:44

    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/

    0 讨论(0)
  • 2020-12-23 16:45

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

    0 讨论(0)
  • 2020-12-23 16:47

    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"]
    
    0 讨论(0)
提交回复
热议问题