[ruby-core:73897] [Ruby trunk Bug#12091] Freezing a SortedSet breaks Enumerable

From: drewish@...
Date: 2016-02-19 19:27:15 UTC
List: ruby-core #73897
Issue #12091 has been reported by Andrew Morton.

----------------------------------------
Bug #12091: Freezing a SortedSet breaks Enumerable
https://bugs.ruby-lang.org/issues/12091

* Author: Andrew Morton
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.4.0dev (2016-02-20 trunk 53876) [x86_64-darwin15]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
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
```



-- 
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>

In This Thread

Prev Next