Skip to content

Instantly share code, notes, and snippets.

@TrevorS
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save TrevorS/55c3a8a4151c9b55732e to your computer and use it in GitHub Desktop.

Select an option

Save TrevorS/55c3a8a4151c9b55732e to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
class Counters
def initialize
@h = Hash.new(0)
end
def increment(key, value = 1)
@h[key] += value
end
def print
puts @h.inspect
end
end
c = Counters.new
c.increment(:a)
c.increment(:a, 1)
c.increment(:b, 3)
c.print
# {:a=>2, :b=>3}
@Wenchao21
Copy link

good,man.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment