60

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?

nosklo
  • 217,122
  • 57
  • 293
  • 297

9 Answers9

63

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

object.methods.sort
Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106
23

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"]
Commander Keen
  • 742
  • 6
  • 12
  • Thank ever so much, am learning Ruby coming from Python and this is a great help. – edef Oct 08 '09 at 18:34
  • 1
    Just curious about your method name, what's a "local" method? Since global methods are static methods, I'm guessing local methods are instance methods? – Dennis Mar 24 '14 at 21:24
5

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.

method
  • 782
  • 4
  • 10
3

The methods method will list all the methods that can be called on the object. It lists all the methods that the object's class defines.

>> "a string".methods
=> [:unicode_normalize, :unicode_normalize!, :ascii_only?, :to_r, :encode, ... ]

There are other methods like this, such as instance_methods which you can read about in the docs: e.g https://ruby-doc.org/core-2.7.3/Module.html#instance_methods-method

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
dylanfm
  • 6,292
  • 5
  • 28
  • 29
  • 2
    This is wrong. This does *not* "list[s] all the methods the module defines". This lists all the methods that can be *called* on `Enumerable`, IOW it lists all the methods that `Enumerable`'s *class* (which is `Module`) defines. – Jörg W Mittag Jan 04 '20 at 15:47
  • I updated the answer with the @JörgWMittag's comment. – Rob Bednark Sep 11 '22 at 17:06
1

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/

Tim
  • 12,318
  • 7
  • 50
  • 72
1
y String.methods.sort

gives a yaml representation of the sorted array of methods. Note that this can be used to list the methods of both classes and objects.

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Codebeef
  • 43,508
  • 23
  • 86
  • 119
0

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.

rampion
  • 87,131
  • 49
  • 199
  • 315
0

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.

jshen
  • 11,507
  • 7
  • 37
  • 59
0

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

Keltia
  • 14,535
  • 3
  • 29
  • 30