Writing ruby code that only executes in development mode
November 11, 2008 § Leave a comment
Our application does a lot of parsing of tree structures in development mode which aids our application developers in creating squeeky-clean code. However, in production we don’t really want and don’t need the overhead of all these additional recursive tree-parsing methods, but we also don’t want to change the application code.
The answer is to leave the application code making calls to the platform, but adapting the methods in the platform to only execute the method bodies in development mode.
We have a module that is mixed in where appropriate:
module DefineInDevelopment # Defines a method used to define other methods that are only available in development mode if ENV['RAILS_ENV'] == "development" def define_in_development yield end else def define_in_development end end end
Then in any method where we only want the processing overhead in development, we wrap the method’s body in a define_in_development method call passing a block:
include DefineInDevelopment def my_expensive_method_for_development(arg1, arg2) do define_in_development do # really expensive code end end
Simple, but incredibly effective! Exploring this also identified a performance gotcha which I discuss in my next blog entry.
Original post: Writing ruby code that only executes in development mode.