Guest User

Untitled

a guest
May 28th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.31 KB | None | 0 0
  1. def adder(n)
  2. v = 0 # v is lexically scoped here
  3. lambda { v += n } # v and n are accessed from the closure
  4. end
  5.  
  6. add1 = adder(10)
  7. p add1.call #=> 10
  8. p add1.call #=> 20
  9. p add1.call #=> 30
  10.  
  11. add2 = adder(5)
  12. p add2.call #=> 5
  13. p add2.call #=> 10
  14. p add2.call #=> 15
Add Comment
Please, Sign In to add comment