Ruby equivalent of Python's "dir"? Ruby equivalent of Python's "dir"? python python

Ruby equivalent of Python's "dir"?


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

object.methods.sort


I like to have this in my .irbrc:

class Object  def local_methods    (methods - Object.instance_methods).sort  endend

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"]


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.