I have a string as
classname = "Text"
using this I want to create an object of the Text class
Now when I try doing this
classname = classname.constantize
I get the Text as a module and not as a class. Please suggest something.
Thanks and regards
Rohit
You could use:
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"
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