Skip to content

Instantly share code, notes, and snippets.

@reinh
Created December 2, 2011 19:31
Show Gist options
  • Save reinh/1424519 to your computer and use it in GitHub Desktop.
Save reinh/1424519 to your computer and use it in GitHub Desktop.
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