Created
October 1, 2015 08:01
-
-
Save dd1994/105e6d7543bad1a27f22 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
def word_count(str) | |
str_ary = str.split(" ") | |
str_hash = Hash.new(0) | |
str_ary.each do |s| | |
str_hash[s] += 1 | |
end | |
return str_hash | |
end | |
puts word_count("hello hello world world world pa") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment