Convert string to class name without using eval in ruby?

后端 未结 2 2040
庸人自扰
庸人自扰 2020-12-31 09:41

I have something like this:

string = \"Post\"

I would like to convert the string to a class name literal. I use eval like this to convert t

2条回答
  •  梦谈多话
    2020-12-31 10:13

    You can try

    class Post
    end
    
    Object.const_get("Post")
    

    Which returns the Post class

提交回复
热议问题