ruby picture

RCR 336: Allow rescue, else, and ensure clauses on do/end blocks

Submitted by Daniel Schierbeck (Thu May 18 15:47:01 UTC 2006)

Abstract

Oftentimes, one may need to add error handling to the content of a do/end block, i.e. add a begin/end block inside it. If rescue, else, and ensure clauses were allowed on do/end blocks, this extra code would not be needed.

Problem

Often I find myself writing code like this:

  foo do
    begin
      something_scary
    rescue
      something_not_so_scary
    end
  end

Which in my opinion is verbose -- both class/end and def/end blocks can have rescue, ensure, and else clauses, so why shouldn't do/end clauses?

Proposal

The code above could be written like this, arguably more terse:

  foo do
    something_scary
  rescue
    something_not_so_scary
  end

Furthermore, else and ensure clauses should also be allowed:

  foo do
    something_scary
  rescue
    something_not_so_scary
  else
    we_made_it!
  ensure
    change_pants
  end

The curly bracket blocks would not support this; first of all, there's not really an elegant way to accomplish it. Second, they're usually only used for one-liners (though I know this is not always the case.)

Analysis

This solution would not break backwards compatability -- it's simply an extension to the current syntax.

It will increase the readability and terseness of Ruby code, and will make do/end block behaviour consistent with that of class/end and def/end.

Implementation

Needs to be done in C.
ruby picture
Comments Current voting
Setting up an exception handler for every block invocation will be very expensive.


Setting up an exception handler for every block is *not* necessary -- only blocks with rescue clauses will have exception handling.


When I first saw a method def with a rescue I actually assumed that a rescue clause was supported for all block constructs. I wasn't until a couple of days later when I tried it on a if/end and it failed that I realized that it was a special case for method and class defs. Since then, I have often wished I could add rescue clauses to block structures that are currently do not support it.

I like adding optional rescue clauses to do/end but I think that rescue clauses should be supported on all syntax block constructs. I think that the same arguments for having rescue clauses on methods and class defs can be applied to having them on any block construct. And having the everywhere makes it easier to explain and think about (for me at least).

Peter


Peter: there's a problem with if and unless blocks and rescue clauses.

  # this is valid code
  begin
    # do something
  rescue
    # if there's an exception
  else  # <---- uh-oh
    # if there are no exceptions
  end

See the `else' part? That would make if and unless blocks look odd:

  if foo == bar
    # do something
  else
    # do something else
  rescue
    # if there's an exception
  else
    # if there are no exceptions
  end

Daniel Schierbeck


This should not apply to just do/end, but all *block* forms (which would exclude the if/unless syntax forms).


 This should not apply to just do/end, but all *block* forms (which would exclude the if/unless syntax forms).

I agree with this and would love to see it. Good idea Daniel.

Brian


Strongly opposed 1
Opposed 1
Neutral 1
In favor 4
Strongly advocate 10
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .