In Python we can "dir" a module, like this:
>>> import re
>>> dir(re)
And it lists all functions in the module. Is there a similar way to do this in Ruby?
In Python we can "dir" a module, like this:
>>> import re
>>> dir(re)
And it lists all functions in the module. Is there a similar way to do this in Ruby?
You can take a module, such as
Enumerable
, and send themethods
method which lists all the methods the module defines. Classes that include this module will respond to these methods.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: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/
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: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).