From: Bulat Ziganshin Date: 2002-11-11T23:46:33+09:00 Subject: Re: help with closure contexts Hello t_gecks, Monday, November 11, 2002, 5:25:50 PM, you wrote: t> Question: Could you please explain to me why the context is copied t> in the first case and not in the second ? because variables holds only references to values. after you execute "a=2" statement, aThing is not changed - it got it's value, reference to 23 at the time of call. Try to reexecute "p1=nTimes(a)" after that to get closure with new aThing In the second example references to @a yields actual value of instance variable. btw, try this code: def nTimes(aThing) return proc { |n| aThing * n } end a = "23" p1 = nTimes(a) puts p1.call(3) a << "4" puts p1.call(4) :))) =>> code t> # copy of the context for a closure t> def nTimes(aThing) t> return proc { |n| aThing * n } t> end t> a = 23 t> p1 = nTimes(a) t> puts p1.call(3) t> a = 2 t> puts p1.call(4) t> # versus reference to the context t> class TestMe t> attr_reader :myProc t> def initialize() t> @myProc = proc { puts @a } t> @a = 0 t> end t> def changeMember() t> @a = 2 t> end t> end t> testMeFirst = TestMe.new t> testMeFirst.myProc.call t> testMeFirst.changeMember t> testMeFirst.myProc.call t> <= code t> results in: t> [bla@blub myTests]$ ruby test1.ruby t> 69 t> 92 t> 0 t> 2 -- Best regards, Bulat mailto:bulatz@integ.ru