Archive

Archive for January 26th, 2010

string to class in ruby on rails

January 26th, 2010 Comments off

There are few ways to have a class from a string, most know are:

  • Kernel.const_get(‘User’)
  • eval(‘User’)
  • ‘User’.constantize

The order in which I have named them is important – a bit important, Kernel.const_get is 10 times faster then constantize and 5 times faster then eval.

The reason of such speed for Kernel.const_get is from it has to maintain list of all constants in the application, tests show it may even behave faster then storing names and Class mapping in a hash.

The difference is not big on simple calls to create just one or two classes, but on heavy loaded systems this might give some more percents of the hardware.

Did You liked this post or maybe not, vote on it at dzone.