From: Jacob Fugal Date: 2005-12-23T03:40:08+09:00 Subject: Re: String > Integer Conversion Problem On 12/22/05, dblack@wobblini.net wrote: > On Fri, 23 Dec 2005, Joel VanderWerf wrote: > > This has the unfortunate side effect of conflating NoMethodErrors that > > are reported for other method calls besides the original obj.meth but > > which occur during that method. What's the best way of handling that? > > Compare exception.backtrace depth with the current backtrace depth? > > I guess, but it gets awfully cluttered. I don't know what the best > way is. Too bad exceptions don't have a depth property or > something.... How about something like this: class Exception def self.local primary = self localized = Class.new klass = (class << localized; self; end) klass.send(:define_method, :===) do |other| primary === other and other.depth == 1 end localized end def depth self.backtrace.length end end Usage: begin bar() rescue NoMethodError.local => e end def bar foo() end begin bar() rescue NoMethodError.local => e end First rescue catches the undefined bar since it's local, second rescue doesn't catch the undefined foo since it's not local. Jacob Fugal