Last Updated: February 25, 2016
·
16.04K
· mriddle

Ruby: Convert string to class

I was performing the same operation on multiple classes and wanted to DRY up my code.

If you have a string with the class name you can retrieve the class object to perform operations with it like so:

In Rails clazz = 'YourClassName'.constantize

In Ruby without Rails clazz = Kernel.const_get('YourClassName')