Feature #7882
closed
Allow rescue/else/ensure in do..end
Added by Anonymous over 13 years ago. Updated over 9 years ago.
Description
The keywords rescue, else and ensure can be used when defining methods like so:
However when using a block delimited by do..end, you must use begin..end as well:
It would be nice to be able to drop the extra begin..end and use rescue, etc. clauses directly:
I cannot think of any ambiguities this syntax would cause, but please correct me if I am wrong.
Updated by nobu (Nobuyoshi Nakada) over 13 years ago
Actions
#1
[ruby-core:52524]
Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 13 years ago
Actions
#2
[ruby-core:52525]
I don't find it that odd, Nobu, although I think most developers would tend to use do-end anyway as we usually do in Ruby when the block span multiple lines.
I like the idea very much actually.
Updated by mame (Yusuke Endoh) over 13 years ago
Actions
#3
[ruby-core:52530]
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
I have suggested the same proposal (in Japanese [ruby-dev:31393]).
Matz said in [ruby-dev:31423] that it is not clear (to him) whether:
should behave like:
or:
--
Yusuke Endoh mame@tsg.ne.jp
Updated by alexeymuranov (Alexey Muranov) over 13 years ago
Actions
#4
[ruby-core:52531]
I've heard of a convention to use { ... } for blocks evaluated for a result and do ... end for blocks evaluated for side effects: http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc
From this point of view, there probably shouldn't be any differences in the syntax inside the two forms of blocks.
Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 13 years ago
Actions
#5
[ruby-core:52532]
Yusuke, I believe it should be the latter. If you want to rescue from the yielding method you have the option of doing it like this in most cases:
with_transaction do
...
rescue
...
end rescue puts 'with_transaction raised outside the yield block'
Updated by phluid61 (Matthew Kerwin) about 13 years ago
Actions
#6
[ruby-core:55696]
mame (Yusuke Endoh) wrote:
I have suggested the same proposal (in Japanese [ruby-dev:31393]).
Matz said in [ruby-dev:31423] that it is not clear (to him) ...
Definitely the latter. The rescue statement in the block should only rescue errors that occur inside the block. This is more apparent if you consider that:
is equivalent to:
Similarly replacing 'while' with a method, such as #each; the 'rescue' in the block should not expect to catch exceptions in the implementation of 'each', only the exceptions raised in the body of the block.
Updated by hsbt (Hiroshi SHIBATA) over 12 years ago
Actions
#7
[ruby-core:60277]
- Target version changed from 2.1.0 to 2.2.0
Updated by shyouhei (Shyouhei Urabe) almost 10 years ago
Actions
#8
- Has duplicate Feature #11337: Allow rescue without begin inside blocks added
Updated by shyouhei (Shyouhei Urabe) almost 10 years ago
Actions
#9
- Has duplicate Feature #12623: rescue in blocks without begin/end added
Updated by nobu (Nobuyoshi Nakada) almost 10 years ago
Actions
#10
[ruby-core:76790]
- Description updated (diff)
Updated by shyouhei (Shyouhei Urabe) over 9 years ago
Actions
#11
- Has duplicate Feature #12906: do/end blocks work with ensure/rescue/else added
Updated by Nondv (Dmitry Non) over 9 years ago
Actions
#12
[ruby-core:79413]
So... Is there any movement?
Updated by nobu (Nobuyoshi Nakada) over 9 years ago
Actions
#13
[ruby-core:79423]
- Status changed from Assigned to Closed