From: Trans Date: 2006-10-06T05:15:10+09:00 Subject: Re: Associating data with a function Phrogz wrote: > Trans wrote: > > I look at this and think, "where the persistant locals?" Well, we don't > > have em but they can be emulatated via instance vars. > > > > class Foo > > def c1 > > tmp1 = (@_tmp1 ||= ExpensiveObject.new) > > tmp2 = (@_tmp2 ||= ExpensiveObject.new) > > # stuff involving tmp1/tmp2 > > result = tmp1 + tmp2 + c2 > > end > > def c2 > > tmp1 = (@_tmp1 ||= ExpensiveObject.new) > > tmp2 = (@_tmp2 ||= ExpensiveObject.new) > > # stuff involving tmp1/tmp2 > > result = tmp1 + tmp2 > > end > > end > > Persistent locals is an interesting concept. Your specific example > fails, however, if I change c1's last line to: > result = tmp1 * c2 + tmp2 > because calculating c2 will change the value in @_tmp2, making the tmp2 > in c1 invalid. You'd need to ensure that each instance variable had a > unique name in each function, which is just about the same as declaring > all the instance variables during initialization or class creation. Right, they should have had differnt names. T.