Actions
Bug #15830
closedSortedSet's lazy setup causes unpredictable behaviour in subclass's initialize
Description
require 'set'
class SortedSetWithoutLowest < SortedSet
def initialize contents=[]
puts "initialize, #{contents}"
super
lowest = to_a[0]
puts "lowest from #{contents} is #{lowest}, removing"
delete lowest
puts "done initializing #{contents} to #{self}"
end
end
3.times { SortedSetWithoutLowest[1,2,3,4,5] }
initialize, [1, 2, 3, 4, 5]
initialize, [1, 2, 3, 4, 5]
lowest from [1, 2, 3, 4, 5] is 1, removing
done initializing [1, 2, 3, 4, 5] to #<SortedSetWithoutLowest: {2, 3, 4, 5}>
lowest from [1, 2, 3, 4, 5] is 2, removing
done initializing [1, 2, 3, 4, 5] to #<SortedSetWithoutLowest: {3, 4, 5}>
initialize, [1, 2, 3, 4, 5]
lowest from [1, 2, 3, 4, 5] is 1, removing
done initializing [1, 2, 3, 4, 5] to #<SortedSetWithoutLowest: {2, 3, 4, 5}>
initialize, [1, 2, 3, 4, 5]
lowest from [1, 2, 3, 4, 5] is 1, removing
done initializing [1, 2, 3, 4, 5] to #<SortedSetWithoutLowest: {2, 3, 4, 5}>
The first such set created ends up with only [3, 4, 5], because SortedSet does some setup and then re-calls initialize (set.rb
line 807). All further such sets are fine. All the sets are fine if any SortedSet has been made before, so calling SortedSet.new
in the subclass's initialize
and discarding the result is a viable workaround.
Files
Actions
Like0
Like0Like0