Skip to content

Instantly share code, notes, and snippets.

Created October 8, 2012 19:41
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 anonymous/3854489 to your computer and use it in GitHub Desktop.
Save anonymous/3854489 to your computer and use it in GitHub Desktop.
Code:
def count_words(str)
counter = Hash.new {|h,k| h[k] = 0}
str.split(/\b/).each {|word| counter[word.downcase]}
end
puts count_words("A guy, a girl, a canal")
#{'a' => 3, 'guy' => 1, 'girl' => 1, 'canal' => 1 }
After execution:
A
guy
,
a
girl
,
a
canal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment