Skip to content

Instantly share code, notes, and snippets.

@mckomo
Created July 21, 2014 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mckomo/4e8e963cc58b303e3612 to your computer and use it in GitHub Desktop.
Save mckomo/4e8e963cc58b303e3612 to your computer and use it in GitHub Desktop.
Ruby counter
class Counter
def initialize
@counter = "0"
end
def hit
@counter.replace( increment_string( @counter ) ); @counter.dup
end
def total
@counter
end
private
def increment_string( str )
( str.to_i + 1 ).to_s
end
end
c = Counter.new
h1 = { current: c.hit, total: c.total }
h2 = { current: c.hit, total: c.total }
p h1 # {:current=>"1", :total=>"2"}
p h2 # {:current=>"2", :total=>"2"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment