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')
Written by Matthew Riddle
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#