Ahh Ruby... Suppose you have a string class that corresponds to a class name and you want to call a certain method on it, what do you do? Well you could do
but that will be slow (as evals are slow) and apparently that looks ugly.
But the creator of Ruby must have run into that issue often. Because what you can do, is
This is an inflector that tries to find a declared constant with the name in the string. And in Ruby classes are constants...
eval class + '.method'
but that will be slow (as evals are slow) and apparently that looks ugly.
But the creator of Ruby must have run into that issue often. Because what you can do, is
class.constantize.method
This is an inflector that tries to find a declared constant with the name in the string. And in Ruby classes are constants...
Comments
Post a Comment