I was helping with an answer in this question and it sparked a question of my own.
Pie
is an object that has apieces
array made of ofPiePiece
objects.- Each
PiePiece
has aflavor
attribute
How do I create a hash that looks like this:
# flavor => number of pieces
{
:cherry => 3
:apple => 1
:strawberry => 2
}
This works, but I think it could be improved
def inventory
hash = {}
pieces.each do |p|
hash[p.flavor] ||= 0
hash[p.flavor] += 1
end
hash
end
Any ideas?