Issue #17739 has been updated by chrisseaton (Chris Seaton).


I think the point of `#sort!` is that it sorts in-place - so not creating a copy. If we want to be able to restore the unsorted array after an exception happens half-way through sorting, we'd need to create a copy to either swap back in after an exception, or to sort and then swap in on success. If we did either of those... we might as well not have `#sort!` at all and advise people to just use `#sort`, as you'd never really be 'sorting in place'.

----------------------------------------
Bug #17739: Array#sort! changes the order even if the receiver raises FrozenError in given block
https://bugs.ruby-lang.org/issues/17739#change-91027

* Author: kachick (Kenichi Kamiya)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
I think this is a similar issue of https://bugs.ruby-lang.org/issues/17736

```ruby
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. 



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>