From: Gregory Brown Date: 2009-03-03T00:53:42+09:00 Subject: Re: Rub 1.9: "inline rescue" doesn't work? On Mon, Mar 2, 2009 at 10:38 AM, Ken Bloom wrote: > The problem with the rescue modifier is that it lumps all types of errors > into one relatively blunt tool. A rescue(Type) modifier would really > help, and a ?. operator (like Groovy's) would resolve one of the most > common cases for a rescue modifier. Right, if you could do: obj.capitalize rescue(NoMethodError) 42 it'd remove the debugging issue. However, this does force the interpreter to do a whole lot of extra work raising and rescuing an error unnecessarily. Groovy's ?. looks about right in terms of what we really want most of the time rescue is used as a conditional modifier. In the past, I've implemented this as: class Object def try(sym, *args) return if nil? send(sym, *args) if respond_to?(sym) end end then, you get: a = obj.try(:capitalize) || 42 But there are obviously some other limitations here... -greg -- Technical Blaag at: http://blog.majesticseacreature.com Non-tech stuff at: http://metametta.blogspot.com "Ruby Best Practices" Book now in O'Reilly Roughcuts: http://rubybestpractices.com