Project

General

Profile

Bug #12091

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

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: 

 ```ruby 
 ``` 
           def to_a 
             (@keys = @hash.keys).sort! unless @keys 
             @keys 
           end 
 ```

Back