Created
October 8, 2012 19:41
-
-
Save anonymous/3854489 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
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