From: "Ara.T.Howard" Date: 2005-09-29T03:47:39+09:00 Subject: Re: creating independent lambdas in loops On Thu, 29 Sep 2005, Eric Mahurin wrote: > --- Martin DeMello wrote: > >> Ara.T.Howard wrote: >>> On Thu, 29 Sep 2005, David A. Black wrote: >>>> >>>> a = 1 >>>> method_call {|a| a = 2; b = 3 } >>>> puts a # 1 >>>> puts b # 3 >>> >>> oh gawd - that's horrible : variables exported from method >> scope to the global >>> (or enclosing perhaps?) one. eeks! >> >> Blocks exist in the caller's scope, not the method's, so I >> don't really >> see the problem here. >> >> martin > > I like it. The variable locality is independent of whether or > not the variable existed in the outside scope (it is dependent > on that now). Arguments would be local and other variables > would be in the context of what contains the block. Period. > With this you could easily create a localize method: > > def localize(&block) > yield(*Array.new(block.arity)) # arity can't be <0 > end > > Then this would localize x and y for you (initialize to nil): > > localize { |x,y| ... } but it totally violates structured programming principles - methods pollute the heap : data from methods (stack) is not freed when the method returnes. eg: # method which sets up x on the heap method_call { x = 42 } # we cannot free x here - someone may want to print it below p x # we cannot free x here - someone may still want to print it below p x # when can we free it? so all non-local variables would need to live on the heap forever. that'd make it pretty easy to run out of memory in any sort of tight loop. afaikt variables created like that could never be freed because there'd be no way to know if someone would refer to them, at some point in the future, after the method or not. it seems like the only safe way to deal with that sort of thing is pushing/poping them to/from the stack so they are freed on method exit. but maybe i'm missing something obvious here? regards. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna ===============================================================================