Project

General

Profile

Feature #7882

Allow rescue/else/ensure in do..end

Added by Charlie Somerville over 3 years ago. Updated 15 days ago.

Status:
Assigned
Priority:
Normal
[ruby-core:52513]

Description

The keywords rescue, else and ensure can be used when defining methods like so:

def foo
  #
rescue
  #
else
  #
ensure
  #
end

However when using a block delimited by do..end, you must use begin..end as well:

foo do
  begin
    # ...
  rescue
    # ...
    # ...
  end
end

It would be nice to be able to drop the extra begin..end and use rescue, etc. clauses directly:

foo do
  # ...
rescue
  # ...
  # ...
end

I cannot think of any ambiguities this syntax would cause, but please correct me if I am wrong.


Related issues

Duplicated by Ruby trunk - Feature #11337: Allow rescue without begin inside blocks Closed
Duplicated by CommonRuby - Feature #12623: rescue in blocks without begin/end Closed

History

#1 [ruby-core:52524] Updated by Nobuyoshi Nakada over 3 years ago

I remember I've seen the same proposal.

What do you think about {} block?

foo {
  ...
rescue
  ...
}

seems odd to me a little.

Or improve do...end only?

#2 [ruby-core:52525] Updated by Rodrigo Rosenfeld Rosas over 3 years ago

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.

#3 [ruby-core:52530] Updated by Yusuke Endoh over 3 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

I have suggested the same proposal (in Japanese ).
Matz said in that it is not clear (to him) whether:

loop do
  :
rescue
  :
ensure
  :
end

should behave like:

begin
  loop do
    :
  end
rescue
  :
ensure
  :
end

or:

loop do
  begin
    :
  rescue
    :
  ensure
    :
  end
end

--
Yusuke Endoh mame@tsg.ne.jp

#4 [ruby-core:52531] Updated by Alexey Muranov over 3 years ago

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.

#5 [ruby-core:52532] Updated by Rodrigo Rosenfeld Rosas over 3 years ago

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'

#6 [ruby-core:55696] Updated by Matthew Kerwin about 3 years ago

mame (Yusuke Endoh) wrote:

I have suggested the same proposal (in Japanese ).
Matz said in 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:

loop do
rescue
finally
end

is equivalent to:

x = proc do
rescue
finally
end
while true
  x.call
end

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.

#7 [ruby-core:60277] Updated by Hiroshi SHIBATA over 2 years ago

  • Target version changed from 2.1.0 to current: 2.2.0

#8 Updated by Shyouhei Urabe 22 days ago

  • Duplicated by Feature #11337: Allow rescue without begin inside blocks added

#9 Updated by Shyouhei Urabe 22 days ago

  • Duplicated by Feature #12623: rescue in blocks without begin/end added

#10 [ruby-core:76790] Updated by Nobuyoshi Nakada 15 days ago

  • Description updated (diff)

Also available in: Atom PDF