Skip to content

Instantly share code, notes, and snippets.

@ryanveroniwooff
Last active February 1, 2017 00:07
Show Gist options
  • Save ryanveroniwooff/7e9868c2a2a5a870dbd86c29fa0a5d97 to your computer and use it in GitHub Desktop.
Save ryanveroniwooff/7e9868c2a2a5a870dbd86c29fa0a5d97 to your computer and use it in GitHub Desktop.
Word Counting Algorithm - Utilizing the Hash
def count_words(s)
return nil if s.nil? || s.split.empty?
s.downcase.gsub(/[^a-z0-9'!\s-]/i, '').split(" ").each_with_object(Hash.new(0)){ |word,counts| counts[word] += 1 }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment