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 the string:
eval(string) #=> Post
Being a javaScript developer I try to avoid eval. Is there a better way of doing this in Ruby? Or is using eval the preferred way of handling this?
You can try
class Post
end
Object.const_get("Post")
Which returns the Post
class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With