Buy @ Amazon

Search This Blog

March 26, 2015

Blocks not Parameters for logging debug information in Ruby

Okay, it really matters if you've got to log your debug information that is more than a plain dumb string without any concatenation.

Let's take our typical habit of writing a debug like below:
logger.debug("This is a " + potentially + " expensive operation")

The above snippet could be better written as below:
logger.debug { "This is a " + potentially + " expensive operation" }

The block form is recommended because the code in the block is evaluated only if the debug log is to be written/logged. 

Reference:
[Ruby Doc] How to log a message?

Book Recommendation:
[Book] Ruby Programming: The well-grounded Rubyist