Sample input:
\"I was 09809 home -- Yes! yes! You was\"
and output:
{ \'yes\' => 2, \'was\' => 2, \'i\' => 1, \'home\
def count_words(string) string.scan(/\w+/).reduce(Hash.new(0)){|res,w| res[w.downcase]+=1;res} end
Second variant:
def count_words(string) string.scan(/\w+/).each_with_object(Hash.new(0)){|w,h| h[w.downcase]+=1} end