我需要做这样的事情
class Foo
define perform()
puts 'hello'
end
end
classname = 'Foo'
instance = create_instance(classname)
instance.perform();
这样的事情可能吗?如果是怎么办?
非常感谢!
最佳答案
你可以使用 const_get
:
instance = Object.const_get(classname).new
instance.perform
关于ruby - 在变量 Ruby 中使用类名创建的类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10568173/