Project

General

Profile

Actions

Bug #12091

closed

Freezing a SortedSet breaks Enumerable

Added by drewish (Andrew Morton) about 8 years ago. Updated over 6 years ago.

Status:
Closed
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

Updated by shyouhei (Shyouhei Urabe) about 8 years ago

  • Status changed from Open to Assigned
  • Assignee set to knu (Akinori MUSHA)

Updated by nobu (Nobuyoshi Nakada) about 8 years ago

  • Description updated (diff)
Actions #3

Updated by knu (Akinori MUSHA) over 6 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) over 6 years ago

Fixed, thanks!

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0