-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow flat rescue/ensure/else block in do/end block #5114
Allow flat rescue/ensure/else block in do/end block #5114
Conversation
Is there any examples where this could be used in the stdlib? I think they would help a lot in making a decision on this PR. |
c8b2f73
to
ce5bb16
Compare
My gut reaction was: "oh, looks sweet". It fits with the Would the following work? spawn do
something
rescue ex
logger.error(ex)
end |
@straight-shoota Thank you very much!! And we should fix them after merged this and released next version. @ysbaddaden Yes, of course! |
Can the formatter handle this? |
@asterite Of course. I did't touch formatter code, but formatter worked fine. That's fantastic. |
Would be nice if we cleaned up the places where rescue was in a block in the stdlib. My first thought was that it doesn't look right because I'm not used to it, but its consistent with the other block types like def and begin, so I think it's fine. Its a bit more visually noisy than the def and begin variants in the language already though. |
Oh, and a formatter spec would be nice even if it wasn't broken before. |
Just a quick thought: What about |
Note: there's also |
True. And |
@RX14 Added formatter spec for this. |
I know Ruby is adding this for 2.5, but after thinking about this a bit more, I wonder if this code is clear: [1, 2, 3].each do |x|
# do something
ensure
# clean up
end Is the |
|
I still 👍 this |
My 2c is that I don't find it that obvious (at a glance) that the ensure will be executed for each block invocation. I like the syntax for blocks that'll execute exactly once, for sure. I think we should be sure to very clearly and explicitly document this behavior. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will be super useful for spawn at the very least. And probably others.
This feature is imported from Ruby 2.5: https://bugs.ruby-lang.org/issues/12906
Now, we can write such a code: