简体   繁体   中英

Dynamically instantiate a Ruby class similar to Java

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

Thanks!

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

如果您正在使用ActiveSupport(即Rails),则会向String添加一个方法来执行此操作:

"String".constantize.new

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM