Thursday, March 03, 2011

Ruby Light #2

This is the work of one of my colleagues here at TestFreaks, and is the coolest little meta-programming trick I've seen in a while!

code_timer_helper.rb
module CodeTimerHelper

# Creates a CodeTimer section around the method
#
def timed_method(timer, method_name, section_name = method_name.to_s)
eigenclass = (class << self; self; end)
eigenclass.class_eval do

define_method method_name do |*args, &block|
timer.start(section_name)
begin
super(*args, &block)
ensure
timer.stop(section_name)
end
end
end
end
end

No comments: