Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

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

 
Reply With Quote
 
 
 
Reply

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



Advertisments