How do I introspect things in Ruby?
Asked Answered
W

4

25

For instance, in Python, I can do things like this if I want to get all attributes on an object:

>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'pydebug', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions']

Or if I want to view the documentation of something, I can use the help function:

>>> help(str)

Is there any way to do similar things in Ruby?

Weissberg answered 22/3, 2010 at 13:57 Comment(2)
Note really an answer, but for core info you may see: ruby-doc.org/coreFumigant
Check this list khelll.com/blog/ruby/ruby-introspectionDolan
H
42

Sure, it's even simpler than in Python. Depending on what information you're looking for, try:

obj.methods

and if you want just the methods defined for obj (as opposed to getting methods on Object as well)

obj.methods - Object.methods

Also interesting is doing stuff like:

obj.methods.grep /to_/

To get instance variables, do this:

obj.instance_variables

and for class variables:

obj.class_variables
Hibbitts answered 22/3, 2010 at 14:5 Comment(8)
As far as documentation this is normally done via rdoc or similar in the shell, and not in the interpreter.Hibbitts
But what if I'm looking for more than just methods? Like what if I want instance or class variables?Weissberg
Also @thenduks - sometimes I'm already in irb and want info on something. It's a bit of a pain to exit out of it, read the rdoc, and then go back into irb.Weissberg
@thenduks - I hope you don't mind. I found the answer to the question in my first comment and edited your post to include it. :-)Weissberg
You probably don't want instance variables or class variables (except under very special circumstances), because they're not part of the class's public API. In ruby, everything is accomplished through methods.Odelia
@Jason RE: rdoc... I never use rdoc at all, personally (I find the web a much better source for docs, I even have --no-rdoc and --no-ri in my gemrc), and I think that is true for most 'rubyists'. If you really want rdoc then I'd suggest just using it in another terminal (which you probably need for other purposes anyway :)). As Brandon said in another answer there is an ri helper in irb: help Number... but it doesn't work for me... which just proves my point about it not being a primary method of docs for rubyists :)Hibbitts
I don't see how multiple calls are "simpler" than a single call.Kurt
That's what I was looking for (+1), but whether this is "even simpler" than in Python is arguable. The possibility to grep through the strings in a "fluent" interface is a great advantage (e.g. just recall the previous command and type at the end, and not go to start, enter dir(, and then go to the end to append ). Maybe there is a new contender among my dektop calculators (including ipython, octave, my own C++ shell, ...)Psalmbook
B
6

If you want all the methods that you can call on something than use

>>> x.methods

If you want some help information then call help before its class

>>> help x.class

Help is a wrapper for ri within irb.

Biskra answered 22/3, 2010 at 14:7 Comment(0)
O
2

If you have an object, and you want to know what methods it responds to, you can run obj.methods (and all of the tricks that thenduks has mentioned on this result.)

If you have a class, you can run klass.methods to see what class methods are availabe, or you can run klass.instance_methods to know what methods are available on instances of that class. klass.instance_methods(false) is useful, becuase it tells you what methods were defined by the class and not inherited.

There's now way to get help text for a method within Ruby the way python does.

Odelia answered 22/3, 2010 at 14:8 Comment(0)
A
2

There's a module called ObjectSpace which is included into each object created in ruby. It holds all of the methods that help you introspect current context of the process. In irb you begin in Object:Main context which is top level context for current irb session. Then you could do something like time = Time.now and then do irb time which would take you into that object's context and you could inspect it from the inside without calling ObjectSpace methods on that object.

Alcyone answered 22/3, 2010 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.