Project

General

Profile

Bug #12198 ยป problem.rb

Piece of code which instantiates that problematic Hash and illustrates the issue - skalee (Sebastian Skalacki), 03/19/2016 01:31 PM

 
require "date"
require "set"

class ReportBuilder
attr_reader :artists, :artist_activity_by_day

def initialize
@artists = Hash.new do |h, artist_name|
h[artist_name] = {concerts: Set.new}
end

@artist_activity_by_day = Hash.new do |h, (artist_name, date)|
absent_on_subjects = Set.new
artists[artist_name][:concerts] << {date: date, subjects: absent_on_subjects}
h[[artist_name, date]] = absent_on_subjects
end
end

def process_concert concert
date, subject = concert.values_at :date, :subject

concert[:musicians].each do |artist|
artist_activity_by_day[[artist, date]] << subject
end
end
end


#####


expectation = {
"Miles Davis" => {
concerts: [
{
date: Date.civil(1980),
subjects: ["Jazz"].to_set,
}
].to_set
}
}


bld = ReportBuilder.new
bld.process_concert(
date: Date.civil(1980),
subject: "Jazz",
musicians: [
"Miles Davis",
].to_set,
)

report = bld.artists

puts expectation.inspect
puts report.inspect
puts expectation == report # false
puts

e, r = expectation["Miles Davis"][:concerts], report["Miles Davis"][:concerts]
puts e.inspect
puts r.inspect
puts e == r # false

# This is where problem happens
e, r = e.instance_variable_get(:@hash), r.instance_variable_get(:@hash)
puts e.inspect
# {{:date=>#<Date: 1980-01-01 ((2444240j,0s,0n),+0s,2299161j)>, :subjects=>#<Set: {"Jazz"}>}=>true}
puts r.inspect
# {{:date=>#<Date: 1980-01-01 ((2444240j,0s,0n),+0s,2299161j)>, :subjects=>#<Set: {"Jazz"}>}=>true}
# identical
puts e.class # Hash
puts r.class # Hash
puts e == r # false
puts e.each_pair.to_a == r.each_pair.to_a # true

r.default_proc = e.default_proc = nil
puts e == r # Also false!
    (1-1/1)