This question is a continuation of this answer.
Below I am trying to reproduce the answer with the real data structure, but I get an extra array for some reason and the elements are swapped.
Question
The two outputs should be the same, so can anyone see what I am doing wrong?
This is correct:
require 'pp'
a = {"A"=>1, "B"=>1, "C"=>1}
b = {"A"=>1, "B"=>1, "D"=>1, "E"=>1}
c = {"D"=>1, "E"=>1, "F"=>1}
keys = Hash.new { |hash, key| hash[key] = [] }
a.each_key { |k| keys[k] << :a }
b.each_key { |k| keys[k] << :b }
c.each_key { |k| keys[k] << :c }
pp keys
which gives
{"A"=>[:a, :b],
"B"=>[:a, :b],
"C"=>[:a],
"D"=>[:b, :c],
"E"=>[:b, :c],
"F"=>[:c]}
Here is the real data structure,
groups = {
"a" => {"A"=>1, "B"=>1, "C"=>1},
"b" => {"A"=>1, "B"=>1, "D"=>1, "E"=>1},
"c" => {"D"=>1, "E"=>1, "F"=>1}
}
keys2 = Hash.new { |hash, key| hash[key] = [] }
groups.each { |k,v| keys2[k] << v.keys }
pp keys2
which gives the incorrect output
{"a"=>[["A", "B", "C"]], "b"=>[["A", "B", "D", "E"]], "c"=>[["D", "E", "F"]]}