There are a few things I would like to understand in how Ruby handles inline error handlers.
Case 1
This is a common use case.
def foo raise Error end bar = foo rescue 1
It works as expected. The expression foo rescue 1 returns 1 and is correctly assigned to bar .
Case 2
Ruby allows you to process arrays, so this behavior seems odd.
baz = 'a' baz, bar = foo rescue [1, 2] # => [1, 2] baz # => 'a' bar # => nil
The expression returns an array [1, 2] , but does not deconstruct or assign it. He completely misses the task at all.
Case 3
However, when you complete the error in brackets, deconstruction works.
baz, bar = (foo rescue [1, 2])
Case 4
Bonus points: increasing the error and trying to process it inline also misses the assignment
baz = raise Error rescue 1
But adding brackets makes it work.
Edit
I tested this on Ruby 1.9.3-p392 and Ruby 2.0.0
Edit 2 :
I added tags to cases
Edit 3 :
Apparently, some people think that this is not a question, so perhaps the name was not obvious enough. Here is the question in the full text:
Why do these inconsistencies arise and why does adding brackets change something?