I have a string as
classname = "Text"
Run Code Online (Sandbox Code Playgroud)
using this I want to create an object of the Text class
Now when I try doing this
classname = classname.constantize
Run Code Online (Sandbox Code Playgroud)
I get the Text as a module and not as a class. Please suggest something.
Thanks and regards
Rohit
Osc*_*Ryz 17
你可以使用:
Object.const_get( class_name )
$ irb
>> class Person
>> def name
>> "Person instance"
>> end
>> end
=> nil
>> class_name = "Person"
=> "Person"
>> Object.const_get( class_name ).new.name
=> "Person instance"
Run Code Online (Sandbox Code Playgroud)