ruby 将字符串中的类名转换为实际类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3464007/
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 23:01:40  来源:igfitidea点击:

ruby convert class name in string to actual class

ruby-on-rails

提问by unsorted

How do I call a class from a string containing that class name inside of it? (I guess I could do case/when but that seems ugly.)

如何从包含该类名的字符串中调用类?(我想我可以做 case/when 但这看起来很难看。)

The reason I ask is because I'm using the acts_as_commentableplugin, among others, and these store the commentable_type as a column. I want to be able to call whatever particular commentable class to do a find(commentable_id)on it.

我问的原因是因为我正在使用acts_as_commentable插件等,这些插件将 commentable_type 存储为一列。我希望能够调用任何特定的可评论类来find(commentable_id)对其进行处理。

Thanks.

谢谢。

回答by Jamie Wong

I think what you want is constantize

我想你想要的是 constantize

That's an RoR construct. I don't know if there's one for ruby core

这是一个 RoR 结构。不知道有没有ruby core的

回答by jtbandes

"Object".constantize # => Object

回答by user664833

Given a string, first call classifyto create a class name (still a string), then call constantizeto try to find and return the class name constant (note that class names are constants).

给定一个字符串,首先调用classify创建一个类名(仍然是一个字符串),然后调用constantize尝试查找并返回类名constant(注意类名是constants)。

some_string.classify.constantize

回答by Edgar Ortega

I know this is an old question but I just want to leave this note, it may be helpful for others.

我知道这是一个老问题,但我只想留下这个笔记,它可能对其他人有帮助。

In plain Ruby, Module.const_getcan find nested constants. For instance, having the following structure:

在普通 Ruby 中,Module.const_get可以找到嵌套常量。例如,具有以下结构:

module MyModule
  module MySubmodule
    class MyModel
    end
  end
end

You can use it as follows:

您可以按如下方式使用它:

Module.const_get("MyModule::MySubmodule::MyModel")
MyModule.const_get("MySubmodule")
MyModule::MySubmodule.const_get("MyModel")

回答by skalee

When ActiveSupport is available (e.g. in Rails): String#constantizeor String#safe_constantize, that is "ClassName".constantize.

当 ActiveSupport 可用时(例如在 Rails 中):String#constantizeString#safe_constantize,即"ClassName".constantize.

In pure Ruby: Module#const_get, typically Object.const_get("ClassName").

在纯 Ruby 中:Module#const_get,通常是Object.const_get("ClassName").

In recent rubies, both work with constants nested in modules, like in Object.const_get("Outer::Inner").

在最近的 rubies 中,两者都使用嵌套在模块中的常量,例如 in Object.const_get("Outer::Inner")

回答by SSR

If you want to convert string to actuall class name to access model or any other class

如果要将字符串转换为实际类名以访问模型或任何其他类

str = "group class"

> str.camelize.constantize 'or'
> str.classify.constantize 'or'
> str.titleize.constantize

Example :
  def call_me(str)
    str.titleize.gsub(" ","").constantize.all
  end

Call method : call_me("group class")

Result:
  GroupClass Load (0.7ms) SELECT `group_classes`.* FROM `group_classes`