Skip to content
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

END not working as in MRI #3522

Closed
jowl opened this issue Dec 7, 2015 · 1 comment
Closed

END not working as in MRI #3522

jowl opened this issue Dec 7, 2015 · 1 comment
Milestone

Comments

@jowl
Copy link
Contributor

jowl commented Dec 7, 2015

BEGIN and END does not work as in MRI, at least not when used with -n and -e. The behavior differs from 1.7.23 to 9.0.4.0, but neither version does what MRI does. I'll give a few examples to try to explain what I expected, and what I got:

MRI (expected behavior):

$ ruby --version
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
$ seq 2 | ruby -ne 'BEGIN { p :begin }; p :middle; END { p :end }'
:begin
:middle
:middle
:end

JRuby 9.0.4.0:

$ ruby --version
jruby 9.0.4.0 (2.2.2) 2015-11-12 b9fb7aa Java HotSpot(TM) 64-Bit Server VM 25.60-b23 on 1.8.0_60-b27 +jit [darwin-x86_64]
$ seq 2 | ruby -ne 'BEGIN { p :begin }; p :middle; END { p :end }'
:middle
:middle
:end
:end

JRuby 1.7.23:

$ ruby --version
jruby 1.7.23 (1.9.3p551) 2015-11-24 f496dd5 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_60-b27 +jit [darwin-x86_64]
$ seq 2 | ruby -ne 'BEGIN { p :begin }; p :middle; END { p :end }'
:begin
:middle
:begin
:middle
:end
:end

It appears as if the END-block gets registered once per input line in both JRuby versions, and the BEGIN-block never in 9.0.4.0 and once per input line in 1.7.23. And if that isn't enough, if I pass --debug to 9.0.4.0, it runs the BEFORE-block once but the END-block twice.

This is not a feature I rely on in any way, and I just happened to stumble upon it and thought I'd report it.

@enebo enebo changed the title BEGIN and END not working as in MRI END not working as in MRI May 23, 2017
@enebo
Copy link
Member

enebo commented May 23, 2017

An update with minor analysis. JRuby 1.7 is EOL and on 9k we now work as expected for BEGIN. END is not specific to -n at all. Each time we execute code which contains and END we just push it onto the end stack. So:

10.times { END { p :end } }

It should print out once but we print out 10 times. We do correctly run the following at_exit 10 times.

10.times { at_exit { p :end } }

So we need some special code for END.

@enebo enebo closed this as completed in 154275c May 23, 2017
@enebo enebo added this to the JRuby 9.2.0.0 milestone May 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants