From: Lionel Thiry Date: 2006-02-09T01:33:22+09:00 Subject: Re: What's the difference between copying and sharing in closure? Sam Kong a �crit : > Hello! > > I read an interview article with matz about closure. > (http://www.artima.com/intv/closures2.html) > > He mentioned that Ruby's closure is a real closure like Lisp. > Local variables are shared between a method and a closure. > > > local variables are shared between the closure and the method. It's a > real closure. It's not just a copy. > ... > Yes, and that sharing allows you to do some interesting code demos, but > I think it's not that useful in the daily lives of programmers. It > doesn't matter that much. The plain copy, like it's done in Java's > inner classes for example, works in most cases. But in Ruby closures, I > wanted to respect the Lisp culture > > > > Can somebody help me understand what he meant? > Closures in other languages are different from Ruby? > And if possible, I want to see the *interesting code demos". > > Thanks. > > Sam > Sharing closure in ruby: class A attr_accessor :a def initialize @a = "value" end def test proc { @a } end end a = A.new block = a.test puts block.call # "value" a.a = "changed value" puts block.call # "changed value" Copying closure in python: class A: def __init__(self): self.a = "value" def test(self): return lambda a=self.a : a a = A() block = a.test print block() # "value" a.a = "changed value" print block() # "value" -- Lionel Thiry Personal web site: http://users.skynet.be/lthiry/