Skip to content

Instantly share code, notes, and snippets.

@cseder
Created May 8, 2017 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cseder/c2433ebb94e072531a7d62ae8cfefd12 to your computer and use it in GitHub Desktop.
Save cseder/c2433ebb94e072531a7d62ae8cfefd12 to your computer and use it in GitHub Desktop.
class String
def word_count
freq = Hash.new(0)
downcase.scan(/\w+/) { |word| freq[word] += 1 }
return freq
end
end
puts %{Dogs dogs dog dog dogs.}.word_count
# {"dogs"=>3, "dog"=>2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment