From: Eric Hodel Date: 2006-01-20T06:34:28+09:00 Subject: Re: Dynamic class instantiation On Jan 19, 2006, at 1:23 PM, Jason wrote: > unknown wrote: >> >> def klass_stamp(hierachy, *a, &b) >> ancestors = hierachy.split(%r/::/) >> parent = Object >> while((child = ancestors.shift)) >> klass = parent.const_get child >> parent = klass >> end >> klass::new(*a, &b) >> end >> >> >> class A; class B;end; end >> >> klass_stamp 'A::B' >> >> hth. >> >> -a > > Actually, I just found this, which is a little simpler, but looks like > it does the same :) > > mod = Module > name.split(/::/).each {|m| mod = mod.const_get(m) } > m = mod.new def Object.path2class(path) # matches a C method that is not exported into Ruby return path.split('::').inject(Object) { |k,n| k.const_get n } end m = Object.path2class('Test::MyClass').new > # Call my method > m.do_something -- Eric Hodel - drbrain@segment7.net - http://segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com