Last active
December 11, 2015 04:58
-
-
Save deitrick/4548726 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
test_str = "Doo bee doo bee doo" | |
counted = Hash[test_str.downcase.split.map { |p| [p, 0] }] | |
#=> {"doo"=>0, "bee"=>0} | |
Desired output (counting number of times a word appears in a string, returned inside of a hash) | |
#=> {'doo' => 3, 'bee' => 2} | |
BOOM | |
h = Hash.new | |
words = test_str.downcase.split | |
words.each do |w| | |
if h.has_key?(w) | |
h[w] = h[w] + 1 | |
else | |
h[w] = 1 | |
end | |
end | |
return h |
fakefarm
commented
Jan 16, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment