I have an array looks like this
a = ["red apple juice", "red mango juice", "red berry juice" "red fish juice",
black berry juice, black potato juice,
yellow bananen juice, yellow ananas juice, yellow onion juice,
and i would like to have
{red => 4, black => 2, yellow => 3}
Use Enumerable#tally:
a = [
"red apple juice",
"red mango juice",
"red berry juice",
"red fish juice",
"black berry juice",
"black potato juice",
"yellow bananen juice",
"yellow ananas juice",
"yellow onion juice"
]
a.map {|s| s.split.first.to_sym }.tally # => {:red=>3, :black=>2, :yellow=>3}