Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Rub 1.9: "inline rescue" doesn't work?

Thread Tools

Rub 1.9: "inline rescue" doesn't work?

Gregory Brown
Posts: n/a
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
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)

then, you get:

a = obj.try(:capitalize) || 42

But there are obviously some other limitations here...


Technical Blaag at:
Non-tech stuff at:
"Ruby Best Practices" Book now in O'Reilly Roughcuts:

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
rub 1.8.1 and $= Patrick Gundlach Ruby 4 01-07-2004 08:06 AM