Issue:
There seems to be a difference in how JRuby and Ruby handle insertion of elements into an array that is being iterated over.
The version of JRuby doesn't seem to matter. Therefore I suspect a fairly old incompatibility
Versions:
- JRuby: jruby 9.2.9.0
- Ruby: 2.6.5
- OS: Darwin RMBP 19.6.0 Darwin Kernel Version 19.6.0: Sun Jul 5 00:43:10 PDT 2020
Test:
rules = [1, 2, 3, 4, 5]
rules.delete_if.with_index do |rule, index|
p rule
rules.insert(index + 1, 2) if rule == 1
true
end
Running the test
Execute the code above with both JRuby and Ruby.
E.g. ruby file_name.rb
Expected Behavior
Ruby
Actual Behavior
JRuby
My guess
I guess that JRuby sets the max_index/end of an iteration at the beginning of an iteration and Ruby adapts that end if an insert occurs.
I do not want to impose this behaviour on JRuby if it is not how it's intended to be. But I expected it to behave like Ruby.