Convert a string to a hash
text = 'The rain in Spain falls mainly in the plain.'
word_count_hash = Hash.new 0
p word_count_hash
text.split(/\W+/).each { |word| word_count_hash[word.downcase] += 1 }
p word_count_hash
# => {"rain"=>1, "plain"=>1, "in"=>2, "mainly"=>1, "falls"=>1,
# "the"=>2, "spain"=>1}
Related examples in the same category