Ruby equivalent of Python's “dir”?

后端 未结 9 1557
温柔的废话
温柔的废话 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:50

    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.

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

    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.

    0 讨论(0)
  • 2020-12-23 17:01

    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.

    0 讨论(0)
提交回复
热议问题