I have an array of hashes like this:
data = [
{group: "A", result: 1},
{group: "B", result: 1},
{group: "A", result: 0},
{group: "A", result: 1},
{group: "B", result: 1},
{group: "B", result: 1},
{group: "B", result: 0},
{group: "B", result: 0}
]
The group will only be either A or B, and the result will only be 1 or 0. I want to count how many times the result is 0 or 1 for each group, i.e., to get a tally like so:
A: result is "1" 2 times
result is "0" 1 time
B: result is "1" 3 times
result is "0" 2 times
I am thinking of storing the actual results in a nested hash, like:
{ a: { pass: 2, fail: 1 }, b: { pass: 3, fail: 2 } }
but this might not be the best way, so I'm open to other ideas here.
What would be the cleanest way to do this in Ruby while iterating over the data only once? Using data.inject or data.count somehow?