Created
May 8, 2017 16:39
-
-
Save cseder/c2433ebb94e072531a7d62ae8cfefd12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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