Submitted by Daniel Schierbeck (Thu May 18 15:47:01 UTC 2006)
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?
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.)
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.
Comments | Current voting | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
RCRchive copyright © David Alan Black, 2003-2005.
Powered by .
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.
See the `else' part? That would make if and unless blocks look odd:
Daniel Schierbeck
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