Created
July 21, 2014 16:50
-
-
Save mckomo/4e8e963cc58b303e3612 to your computer and use it in GitHub Desktop.
Ruby counter
This file contains hidden or 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 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