04 Mar '13, 2am

Dynamic Method Definitions

2013-03-03 @ 16:52 Dynamic Method Definitions TL;DR: depending on your app, using define_method is faster on boot, consumes less memory, and probably doesn’t signigicantly impact performance. Throughout the Rails code base, I typically see dynamic methods defined using class_eval . What I mean by “dynamic methods” is methods with names or bodies that are calculated at runtime, then defined. For example, something like this: class Foo class_eval <<EORUBY, __FILE__, __LINE__ + 1 def wow_#{Time.now.to_i} # ... end EORUBY end I’m not sure why they are define this way versus using define_method . Why don’t we compare and contrast defining methods using class_eval and define_method ? The tests I’ll do here use MRI, Ruby 2.0.0. Definition Performance When defining a method, is it faster to use class_eval or define_method ? Here is a trivial benchmark where we simulate defining 10...

Full article: http://tenderlovemaking.com/2013/03/03/dynamic_method_def...

Tweets

Love my many AeroPressen…

Love my many AeroPressen…

dearcoffeeiloveyou.com 14 Mar '13, 10pm

If you haven’t heard of an Aeropress , you’re missing out on one of the best ways to brew your coffee. This relatively new...

Dynamic Error Pages In Rails

rubyflow.com 13 Mar '13, 6pm

Dynamic Error Pages In Rails Posted by joshnesbitt on March 13, 2013 — 0 comments We've just published a new blog post abo...