Created
August 16, 2009 11:49
-
-
Save marioaquino/168610 to your computer and use it in GitHub Desktop.
This file contains 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
# Uses Ruby 1.9 syntax for lambdas | |
counter = -> initial do | |
val = initial | |
foo = {next: -> {val += 1}, reset: -> {val = initial}} | |
def foo.method_missing(meth, *args) | |
self[meth].call | |
end | |
foo | |
end | |
c = counter.(10) | |
p c.next #=> 11 | |
p c.next #=> 12 | |
p c.reset #=> 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment