Project

General

Profile

Actions

Bug #17739

closed

Array#sort! changes the order even if the receiver raises FrozenError in given block

Added by kachick (Kenichi Kamiya) over 3 years ago. Updated over 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]
[ruby-core:102963]

Description

I think this is a similar issue of https://bugs.ruby-lang.org/issues/17736

array = [1, 2, 3, 4, 5]
begin
  array.sort! do |a, b|
    array.freeze if a == 3
    1
  end
rescue => err
  p err #=> #<FrozenError: can't modify frozen Array: [5, 4, 3, 2, 1]>
end
p array #=> [5, 4, 3, 2, 1]

array = [1, 2, 3, 4, 5]
array.sort! do |a, b|
  break if a == 3
  1
end
p array #=> [3, 4, 2, 1, 5]

Array#sort! raises a FrozenError as expected, but the order is changed. I would expect the order is kept after frozen.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0