Ruby equivalent of Python's “dir”?

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

提交回复
热议问题