Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Non adapting end of iteration on insert during iteration - difference between Ruby and JRuby #6371

@ghost

Description

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

1
2
2
3
4	
5

Actual Behavior
JRuby

1
2
2
3
4

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions