Sunday, March 8, 2009

Thing--a classes functionality reporter in ruby

Alan came over today to talk nerdworldese with me today. The topics of conversation included protein phylogeny algorithms, Python, and using Eclipse for CSV repository synchronization. One thing he showed me in Python that caught my interest was the dir() method/function (whatever Pythoners call their methods), which lets you inspect an the functionality of a module. I hadn't thought of needing that before, probably because ruby has the uber helpful object.methods method to get a list of all applicable methods. But when I saw that you could inspect modules I have to admit I was jealous. Surprisingly, no one has written equivalent functionality into ruby yet. Whelp, I set out make up for lost time. The above shows the code for Thing class, which reports on many of the internals of an object (example: Thing.meth(Array.new)). It is capable of introspection--reporting on the Thing class itself. I also found that one can type help 'Hash' from within irb to access the documentation. I am embarrassed that I didn't realize that way earlier. It is obviously nice not to have to flip to another terminal window just to use ri to look at the documentation.
Tim


No comments: