From: James Britt Date: 2006-12-27T13:46:19+09:00 Subject: Re: Object creation with a "variable classname" Mat Schaffer wrote: > On Dec 26, 2006, at 6:52 PM, James Britt wrote: >> cedric.ch wrote: >>> Hi, >>> I would to know if it is possible to do something like that: >>> def create_object(class_name) >>> my_object = **class_name**.new >>> end >>> I tried with class.new(&class_name) >> >> >> x = Object.const_get( class_name ).new > > Does that still work if there's a module mentioned in the string with > '::' included? I seem to remember a discussion or maybe an eigenclass > blog entry that mentioned the problem. But maybe I'm wrong. > -Mat Well, try it and see: module Foo class Bar def baz warn 'BAZ!' end end end Foo::Bar.new.baz Object.const_get( 'Foo::Bar' ).new.baz http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/160468 -- James Britt http://web2.0validator.com - We're the Dot in Web 2.0 http://www.rubyaz.org - Hacking in the Desert http://www.jamesbritt.com - Playing with Better Toys