Project

General

Profile

Actions

Bug #15479

closed

Array#reject! modifies literal Array

Added by Eregon (Benoit Daloze) about 5 years ago. Updated about 5 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
[ruby-core:90781]

Description

This was found by running ruby/spec with -R2 (such as mspec -R2 -fs core/array/reject_spec.rb).
TravisCI log on https://travis-ci.org/ruby/spec/jobs/473175799#L539

Here is a simple reproducer.
MRI seems to modify the Array literal permanently:

3.times do
  a = [1, 2, 3, 4]
  puts "initial: #{a}"
  begin
    a.reject! do |x|
      case x
      when 2 then true
      when 3 then raise StandardError, 'Oops'
      else false
      end
    end
  rescue StandardError
  end

  puts "after:   #{a}"
end

prints

initial: [1, 2, 3, 4]
after:   [1, 3, 4]
initial: [1, 3, 4, 4]
after:   [1, 3, 4, 4]
initial: [1, 3, 4, 4]
after:   [1, 3, 4, 4]

2.5.3 behaves fine, but trunk is also affected.


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #15349: Use a shared array for the `duparray` instructionClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0