Hey Sonia, classname = "Foo" klass = Object.const_get(classname) foo = klass.new
Regards, Tommy On 18 August 2011 15:12, Sonia Hamilton <[email protected]> wrote: > I'm just wondering about the Ruby way of instantiating a class when the > class name is in a string? > > For example: > > def build_classname > # reality is a bit more complex... > "Foo" + "Builder" > end > class_name = build_classname > > Then I want to instantiate an instance based on class_name, ie the > equivalent of: > > obj = FooBuilder.new > # obj = class_name.new XX FAIL > > Google isn't helping - ruby + classname + instantiate + string brings up > all sorts of random stuff :-) I was thinking of doing some eval > awfulness, but there's got to be a Ruby way... > > -- > Sonia Hamilton > > -- > You received this message because you are subscribed to the Google Groups > "Ruby or Rails Oceania" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/rails-oceania?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
