From: "Mauricio Fernández" Date: 2003-03-21T05:22:37+09:00 Subject: Re: class level Exception handling On Fri, Mar 21, 2003 at 02:58:15AM +0900, Robert Klemme wrote: > Did I miss something or do you store the same block in @block for each > method? If so, why? The original poster wanted to have a single error > handler for all methods. I thought he could perhaps change his mind :) wrap :some_method do foo end wrap :some_other do bar end My code will work if he wants the same block for all methods, and it allows further "expansion". If there was only one handler per class, the usage of the module could be simplified to just class A extend ClassMethodsWrapper #only needed once in the class hierarchy wrap do # optional args, see below # moreover, if properly done, the wrapper can be inherited # too end end with some magic involving * Module#extend_object * Module#method_added I'm not doing that however cause I'm getting a very strong 'd�j� vu' feeling, after writing KeywordProcessor and NoOverloading ;-) I mean, I love playing with singleton classes and so, but using the same tricks again a again spoils the fun. > Another suggestion I would make is to change the rescue clause to: > > rescue Exception => e > return self.class.__block__(:#{meth.to_s}).call(e) > end > > and then > > wrap :foo, :bar, :baz do |ex| > puts "Got #{ex.inspect}" > false > end > > This is more explicit and provides for easier access to the exception, > because you need not remember that the exception is stored in $!. Could be useful; the following too: rescue Exception return self.class.__block__(:#{meth.to_s}).call([:#{meth.to_s}, block] + args) end wrap :foo, :bar, :baz do |meth, block, *args| puts "Got #{$!.inspect} when calling #{meth} with args #{args.inspect}" # can moreover do something with block end -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com C is quirky, flawed, and an enormous success -- Dennis M. Ritchie