Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2012 20:53
Show Gist options
  • Save anonymous/1968177 to your computer and use it in GitHub Desktop.
Save anonymous/1968177 to your computer and use it in GitHub Desktop.
def count_words(str)
words = str.split
counts = words.map { |w| words.count(w) }
Hash[words.zip(counts)]
end
count_words('doo bee doo bee') # => {"doo"=>2, "bee"=>2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment