views:

207

answers:

1

Often I find myself inserting logging statements like this into my code:

logger.info("orphaned_annotations: ".upcase + orphaned_annotations.to_s)
logger.info("orphaned_anchors: ".upcase + orphaned_anchors.to_s)

It seems like this would be a pretty common use-case -- is there an easier way to get this kind of functionality?

+1  A: 

Two common options are: 1. Create a macro in whatever editor your using that when you type in log will auto-complete everything up to the name of the variable, then the name of the variable you type in can be put in both the string and the to_s and of course autocomplete the rest. Most decent editors will provide facilities for making macros like this as long as you can figure out the syntax. 2. The second and probably simpler option is to just define a global helper function like

def debug(name,var)
   logger.info(name.upcase << var.to_s)
end

I wanted to figure out a way to simply pass in the variable and then have ruby figure out the name of the object but I'm not sure there is an easy way to do that (see this question).

Personally I would probably do 1) since figuring out macros can save a lot of time when you have a set of operations you do frequently.

jkupferman