From: David Masover Date: 2010-04-25T00:38:48+09:00 Subject: Re: DrX, an object inspector On Wednesday 21 April 2010 11:20:12 pm Charles Oliver Nutter wrote: > On Tue, Apr 20, 2010 at 10:44 PM, Mooffie n/a wrote: > > David Espada wrote: > >> Any plan to extend it for JRuby?.... 0:D > > > > I'm not familiar with JRuby, but it was always on my mind to look into > > it, so the short answer to your question is "yes". > > > > DrX is composed of three independent components (GUI; GraphViz DOT > > generation; A helper C extension) exactly for this reason. While the > > current GUI, written in Tk, probably isn't portable, it isn't a > > stumbling block. > > For getting the data, you don't even need to write an extension to > JRuby. The "jruby" library gives you introspective access to all > objects at a very direct level: > > require 'jruby' > > # no JRuby on this machine, so I'm doing this from memory > some_str = 'hello!' > ref = JRuby.reference(some_str) > ref.class # now org.jruby.RubyString > ref.methods # now includes all Java-land methods on RubyString > cls = ref.metaclass # an org.jruby.RubyClass instance > cls.methods # should be the internal method table It seems like this should be possible to do from bare Ruby, in a portable way. Clumsy, but possible. For example, as long as people don't get smart and use Ruby 1.9's BasicObject, you can do this: Object.instance_method(:methods).bind('foo').call That takes care of things like Builder's BlankSlate object, though: Object.instance_method(:methods).bind(BlankSlate.new).call => ["__send__", "instance_eval", "__id__"] BlankSlate itself hints at other ways to abuse Ruby for similar effects -- for instance, I couldn't readily find a way to discover where a method was originally defined, and whether it's been overridden, but it seems likely that if I could get my fangs deep into things like method_added before anything else was loaded, I could generate this kind of map. I might be missing what you're trying to do, though.