crystal-lang

How do I create a class instance from a string name in Crystal?


I want the Crystal equivalent of this:

clazz = 'ExampleClass'.constantize # Rails
clazz = Object.const_get('ExampleClass') # pure Ruby
obj = clazz.new

Solution

  • You can't, these kind of dynamic aspects are not possible in Crystal.

    However, macros can take a long way. Please read this similar issue and discussion:

    https://github.com/crystal-lang/crystal/issues/949