On Mon, Mar 2, 2009 at 10:38 AM, Ken Bloom <(E-Mail Removed)> 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
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:
def try(sym, *args)
return if nil?
send(sym, *args) if respond_to?(sym)
then, you get:
a = obj.try(:capitalize) || 42
But there are obviously some other limitations here...