Project

General

Profile

Bug #12091

Freezing a SortedSet breaks Enumerable

Added by drewish (Andrew Morton) almost 4 years ago. Updated about 2 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
ruby -v:
ruby 2.4.0dev (2016-02-20 trunk 53876) [x86_64-darwin15]
[ruby-core:73897]

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

Revision 3090169c
Added by knu (Akinori MUSHA) about 2 years ago

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

Revision 60298
Added by knu (Akinori MUSHA) about 2 years ago

Allow a SortedSet to be frozen and still functional [Bug #12091]

Revision 60298
Added by knu (Akinori MUSHA) about 2 years ago

Allow a SortedSet to be frozen and still functional [Bug #12091]

Revision 60298
Added by knu (Akinori MUSHA) about 2 years ago

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 nobu (Nobuyoshi Nakada) over 3 years ago

  • Description updated (diff)
#3

Updated by knu (Akinori MUSHA) about 2 years ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r60298.


Allow a SortedSet to be frozen and still functional [Bug #12091]

Updated by knu (Akinori MUSHA) about 2 years ago

Fixed, thanks!

Also available in: Atom PDF