Skip to content

Instantly share code, notes, and snippets.

@pricees
Created July 11, 2012 08:04
Show Gist options
  • Save pricees/3088857 to your computer and use it in GitHub Desktop.
Save pricees/3088857 to your computer and use it in GitHub Desktop.
"Counter Hash"
#########################################
#
# ==Keeps count of number of times key has been added.==
#
# Use:
# hsh = Hash.counter_hsh
# hsh << :foo
# hsh << :foo << :foo
# hsh << "foo"
#
# hsh # { :foo => 3, "foo" => 1 }
#
class Hash
def << k
self[k] += 1
self
end
def self.counter_hsh
Hash.new 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment