# something that represents a rake task class Task attr_accessor :name def initialize(name) @name = name end end # something like a rake 'task' method def task(name, &block) yield Task.new(name) end # a method that will execute a block defined inside of a rake task # the idea is that this method can trap and report on exceptions # I want to get the task name to include in the report in as transparent # of a way as possible. Unfortunately, it seems eval is the only way to # go about getting this. def handler(&block) begin yield rescue Exception => e # ideally, this would test for t first name = block.binding.eval('t.name') puts "error in task '#{name}': e.message" end end task "publish" do |t| handler do # stuff here # handler will pick up t raise "something bad happened" end end # this results in # error in task 'publish': e.message