Skip to content

Instantly share code, notes, and snippets.

@JEG2
Created September 10, 2011 02:24
Show Gist options
  • Save JEG2/1207848 to your computer and use it in GitHub Desktop.
Save JEG2/1207848 to your computer and use it in GitHub Desktop.
def closure_with_state(start, multiplier)
i = -1
current = nil
lambda { current = start * (multiplier * (i += 1)) }
end
doubles = closure_with_state(1, 2)
triples = closure_with_state(1, 3)
p Array.new(10) { doubles.call }
p Array.new(10) { triples.call }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment