I am new to ruby and looking for an elegant way to sort the following hash.
Hash A:
a = { :metric1 => {
"Bob" =>10,
"Jane" =>15,
"Sally" =>20
},
:metric2 => {
"Jane" =>15,
"Bob" =>10,
"Sally" =>10
},
:metric3 => {
"Bob" =>10,
"Sally" =>10,
"Jane" =>5
}
}
Hash B:
b = { "Bob" => {
:metric1 =>10,
:metric2 =>10,
:metric3 =>10
},
"Jane" => {
:metric1 =>15,
:metric2 =>15,
:metric3 =>5
},
"Sally" => {
:metric1 =>20,
:metric2 =>10,
:metric3 =>10
}
}
Essentially I want an easy way to iterate this object to output a table like:
Metric 1 Metric 2 Metric 3
Bob 10 20 20
Jane 15 15 5
Sally 20 10 10
Functional approach (assumes all people have the same metric keys):
[Edit] A second approach, proably more orthodox than my first attempt. It uses Facets (so it may look a bit cryptic for those unfamiliar the new abstractions it procides). However, it’s easy if you check the output of each step: