From: Robert Klemme Date: 2004-07-26T22:16:53+09:00 Subject: Re: code introspection "Lothar Scholz" schrieb im Newsbeitrag news:178359988968.20040726131022@scriptolutions.com... > Hello Daniel, > > DC> Is there a solution to do code introspection rather than introspection > DC> at the level of the Module/Object ? I can't figure out if I'm looking > > Yes but only from the C level. You need to create the "Node" tree and > travere it. Unfortuneatly there seems to be no visitor pattern class to do > this and you must expect that the Node structure can change even > between minor updates a lot. So read the code in eval.c and parse.c. Err, I think this is overkill. Daniel just wants to create an instance of a class whose name he knows. So the already suggested Object.const_get( class_name ) or ObjectSpace.const_get( class_name ) suffices. If you don't know class names beforehand, you can work with an anonymous module as wrapper like this: mod = Module.new mod.class_eval( File.read( "foo.rb" ) ) new_classes = mod.constants.inject([]) {|ar,c| cl=mod.const_get(c); ar << cl if Class === cl; ar} p new_classes include mod p Foo Or even do this mod = Object mod.class_eval( File.read( "foo.rb" ) ) classes = mod.constants.inject([]) {|ar,c| cl=mod.const_get(c); ar << cl if Class === cl; ar} p classes p Foo Regards robert