我正在从这样的字符串加载一个类 Kernel.const_get(some_class_name).new 哪个有效,但这样做的正确方法是什么? 更重要的是,有没有办法卸载课程?我试过了 Object.send(:remove_const, some_class_name) 但
Kernel.const_get(some_class_name).new
哪个有效,但这样做的正确方法是什么?
更重要的是,有没有办法卸载课程?我试过了
Object.send(:remove_const, some_class_name)
但这与上课之前的情况不一样.
注意:与this有关.
既然您使用Rails标记了问题,那么您可以这样做some_class_name.constantize
例
"User".constantize # => User
remove_const有效,但您需要从定义常量的对象空间中调用它.另外,请记住将符号作为常量名称传递.
例
Object.send(:remove_const, :Foo)