Bug #12091
Freezing a SortedSet breaks Enumerable
Description
If you freeze a SortedSet
it looses most functionality because of the way to_a
is implemented:
irb(main):001:0> require 'set' => true irb(main):002:0> Set.new.freeze => #<Set: {}> irb(main):003:0> SortedSet.new.freeze RuntimeError: can't modify frozen SortedSet from /Users/andrew/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/set.rb:679:in `to_a' from /Users/andrew/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/set.rb:541:in `inspect' from /Users/andrew/.rbenv/versions/2.3.0-dev/bin/irb:11:in `<main>'
And since each
method relies on to_a
, all the Enumerable
methods are also broken:
irb(main):006:0> SortedSet.new([1, 2, 2]).freeze.max RuntimeError: can't modify frozen SortedSet from /Users/andrew/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/set.rb:679:in `to_a' from /Users/andrew/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/set.rb:674:in `each' from (irb):6:in `max' from (irb):6 from /Users/andrew/.rbenv/versions/2.3.0-dev/bin/irb:11:in `<main>'
The relevant code is:
def to_a
(@keys = @hash.keys).sort! unless @keys
@keys
end
Associated revisions
Allow a SortedSet to be frozen and still functional [Bug #12091]
Allow a SortedSet to be frozen and still functional [Bug #12091]
Allow a SortedSet to be frozen and still functional [Bug #12091]
History
Updated by shyouhei (Shyouhei Urabe) over 3 years ago
- Status changed from Open to Assigned
- Assignee set to knu (Akinori MUSHA)
Updated by knu (Akinori MUSHA) about 2 years ago
- Status changed from Assigned to Closed
Allow a SortedSet to be frozen and still functional [Bug #12091]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60298 b2dd03c8-39d4-4d8f-98ff-823fe69b080e