Ruby-on-rails 如何从Rails中的名称字符串实例化类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14070369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:13:51  来源:igfitidea点击:

How to instantiate class from name string in Rails?

ruby-on-railsruby-on-rails-3stringclassinstantiation

提问by Vjatseslav Gedrovits

How we can instantiate class from it's name string in Ruby-on-Rails?

我们如何从 Ruby-on-Rails 中的名称字符串实例化类?

For example we have it's name in database in format like "ClassName" or "my_super_class_name".

例如,我们在数据库中以“ClassName”或“my_super_class_name”等格式存储它的名称。

How we can create object from it?

我们如何从中创建对象?

Solution:

解决方案:

Was looking for it myself, but not found, so here it is. Ruby-on-Rails API Method

自己也在找,没找到,所以来了。 Ruby-on-Rails API 方法

name = "ClassName"
instance = name.constantize.new  

It can be even not formatted, we can user string method .classify

它甚至可以不格式化,我们可以使用字符串方法.classify

name = "my_super_class"
instance = name.classify.constantize.new

Of course maybe this is not very 'Rails way', but it solves it's purpose.

当然,也许这不是“Rails 方式”,但它解决了它的目的。

回答by Eugene Rourke

klass = Object.const_get "ClassName"

about class methods

关于类方法

class KlassExample
    def self.klass_method
        puts "Hello World from Class method"
    end
end
klass = Object.const_get "KlassExample"
klass.klass_method

irb(main):061:0> klass.klass_method
Hello World from Class method

回答by maninalift

Others may also be looking for an alternative that does not throw an error if it fails to find the class. safe_constantizeis just that.

其他人也可能正在寻找一种替代方法,如果无法找到类,则不会抛出错误。safe_constantize就是这样。

class MyClass
end

"my_class".classify.safe_constantize.new #  #<MyClass:0x007fec3a96b8a0>
"omg_evil".classify.safe_constantize.new #  nil 

回答by Pushp Raj Saurabh

You can simply convert a string and initialize a class out of it by:

您可以简单地转换一个字符串并通过以下方式初始化一个类:

klass_name = "Module::ClassName"
klass_name.constantize

To initialize a new object:

初始化一个新对象:

klass_name.constantize.new

I hope this turns out to be helpful. Thanks!

我希望这会有所帮助。谢谢!

回答by RubesMN

I'm surprised nobody is considering security and hacking in their responses. Instantiation of an arbitrary string that likely came even indirectly from user input is asking for trouble and hacking. We all should/must be whitelisting unless we're sure the string is fully controlled and monitored

我很惊讶没有人在他们的回复中考虑安全性和黑客攻击。甚至可能间接来自用户输入的任意字符串的实例化是自找麻烦和黑客攻击。我们都应该/必须加入白名单,除非我们确定字符串是完全控制和监控的

def class_for(name)
  {
    "foo" => Foo,
    "bar" => Bar,
  }[name] || raise UnknownClass
end

class_for(name_wherever_this_came_from).create!(params_somehow)

How you would know the appropriate params arbitrarily without having a whitelist would be challenging but you get the idea.

如何在没有白名单的情况下任意知道适当的参数将具有挑战性,但您明白了。