This is Rails, not Ruby - hopefully that is okay: class_name = build_classname.constantize class_name.new
-- Pat On 18/08/2011, at 3:12 PM, Sonia Hamilton 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.
