I have a Ruby array that looks something like this:
animals = %w(dog cat bird cat dog bird bird cat)
I need to get a count of each unique item in the array. I could do something like this:
dogs = 0
cats = 0
birds = 0
animals.each do |animal|
dogs += 1 if animal == 'dog'
cats += 1 if animal == 'cat'
birds += 1 if animal == 'bird'
end
...but this approach is too verbose. What is the most concise way of calculating these unique counts in Ruby?