在变量Ruby中使用类名创建的类实例

yet*_*ety 4 ruby

我需要做那样的事情

class Foo
 define perform()
  puts 'hello'
 end
end

classname = 'Foo'

instance = create_instance(classname)
instance.perform();
Run Code Online (Sandbox Code Playgroud)

这样的事情是可能的.如果是这样的?

非常感谢!

rjz*_*rjz 5

你可以使用const_get:

instance = Object.const_get(classname).new
instance.perform
Run Code Online (Sandbox Code Playgroud)