Created
December 2, 2011 19:31
-
-
Save reinh/1424519 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
class CountHash < Hash | |
def initialize | |
super(0) | |
end | |
def count(key) | |
self[key] += 1 | |
self | |
end | |
end | |
p 'these are some words and these are some more words'.split.reduce(CountHash.new, :count) | |
# => {"these"=>2, "are"=>2, "some"=>2, "words"=>2, "and"=>1, "more"=>1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment