SHARE
TWEET

Example of Proc objects preserving local context

a guest Jun 19th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Example of Proc objects preserving local context
  2.  
  3. def counter
  4.   n = 0
  5.   return Proc.new { n+= 1 }
  6. end
  7.  
  8. a = counter
  9. a.call            # returns 1
  10. a.call            # returns 2
  11.  
  12. b = counter
  13. b.call            # returns 1
  14.  
  15. a.call            # returns 3
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top