tags:

views:

1180

answers:

2

How can this line in Java be translated to Ruby:
String className = "java.util.Vector";
...
Object o = Class.forName(className).newInstance();

Thanks!

+16  A: 

Object::const_get('String').new()

Ken
If you'd like to instantiate a class inside a module you just use the module instead of `Object`. `MyCoolModule::Submodule.const_get('MyString').new`
ba
+3  A: 

If you need to do this in a Rails project, there is also a helper that does this:

"String".constantize.new
Ian Terrell