Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def adder(n)
- v = 0 # v is lexically scoped here
- lambda { v += n } # v and n are accessed from the closure
- end
- add1 = adder(10)
- p add1.call #=> 10
- p add1.call #=> 20
- p add1.call #=> 30
- add2 = adder(5)
- p add2.call #=> 5
- p add2.call #=> 10
- p add2.call #=> 15
Add Comment
Please, Sign In to add comment