Skip to content

Instantly share code, notes, and snippets.

@marioaquino
Created August 16, 2009 11:49
Show Gist options
  • Save marioaquino/168610 to your computer and use it in GitHub Desktop.
Save marioaquino/168610 to your computer and use it in GitHub Desktop.
# 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