From: John Johnson Date: 2006-04-29T05:53:55+09:00 Subject: Re: how to create Class object with name determined at runtime You can use eval() to do that, like so: irb(main):001:0> a='String' => "String" irb(main):002:0> b=eval("#{a}.new") => "" irb(main):003:0> b.class => String irb(main):004:0> In the second irb line, the contents of the variable _a_ are substituted in the string before the .new. It amounts to b=eval("String.new"). Regards, JJ On Friday, April 28, 2006, at 02:55PM, Bill Roberts wrote: >I hope someone can help with this - I am a Ruby newbie. > >In my code I want to decide at runtime which type of object to create, >based on a variable holding the name of the class. > >So I have classes Foo and Bar and a string todays_class. > >If todays_class = "Foo" I want to say my_obj = Foo.new >if todayS_class = "Bar" then my_obj = Bar.new > >Except I've got a lot more than two classes and I'd prefer not to use an >if or case statement, so that I don't need to change the code if I add >another one to the list. > >Any suggestions on a neat way to do this? > >Thanks > >-- >Posted via http://www.ruby-forum.com/. > > > --- Help everyone. If you can't do that, then at least be nice.