Reputation: 2792
I have a variable that stores the name of a class
my_class = "Homework"
This class has certain attributes which I would like to access
Homework.find_by
How can I make ruby see this string as an object?
e.g.
my_class.find_by
Upvotes: 2
Views: 89
Reputation: 4615
You can use classify
and constantize
my_class.classify.constantize.find_by # something
classify
Create a class name from a plural table name like Rails does for table names to models. Note that this returns a string and not a Class (To convert to an actual class follow classify with constantize).
constantize
Tries to find a constant with the name specified in the argument string.
'Module'.constantize # => Module
'Test::Unit'.constantize # => Test::Unit
If you are sure about your input, you need only constantize
my_class.constantize.find_by # something
Upvotes: 3