How To

How To Get Word Count / Frequencies using Hashes in Ruby

wf = Hash.new(0).tap { |h| words.each { |word| h[word] += 1 } }

Using Enumerable#each_with_object:

wf = words.each_with_object(Hash.new(0)) { |word, acc| acc[word] += 1 }

A functional/immutable approach:

wf = words.group_by(&:itself).map { |w, ws| [w, ws.length] }.to_h

Ruby 2.7 onwards will have the Enumerable#tally method that will solve:

Tallys the collection. Returns a hash where the keys are the elements and the values are numbers of elements in the collection that correspond to the key.

["a", "b", "c", "b"].tally #=> {"a"=>1, "b"=>2, "c"=>1}