From: Joel VanderWerf Date: 2006-04-18T03:39:17+09:00 Subject: Re: Refactoring (#75) Jim Weirich wrote: > Dave Burt wrote: >> Hi Ruby Quiz, >> >> Sorry, this is likely to be rather uninteresting, > > Actually, I find it very interesting ... but then, you have to consider > my tastes. :) > >> Refactoring 1: Merge Redundant Exception Handlers >> /You have a series of exception handling blocks with identical >> handlers./ >> *Merge the code into a single exception handling block.* >> >> Thus: >> >> begin >> foo >> rescue Exception => e >> barf e >> end >> begin >> bar >> rescue Exception => e >> barf e >> end >> >> Becomes: >> >> begin >> foo >> bar >> rescue Exception => e >> barf e >> end > > You need to be careful here. If 'barf' does not thrown an exception, > but merely prints an error message of some kind, then the refactoring > changes the behavior (which technically, makes it *not* a refactoring). What about using another method and yield here? def barf_on_exception yield rescue Exception => e barf e end barf_on_exception {foo} barf_on_exception {bar} Or even: def on(ex, m) yield rescue ex => e send m, e end on(Exception, :barf) {foo} on(Exception, :barf) {bar} But I'm not sure that it's really better than the original exception clause. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407