From: "Mauricio Fernández" Date: 2004-08-24T19:47:52+09:00 Subject: Re: *with* block? On Tue, Aug 24, 2004 at 07:24:07PM +0900, T. Onoma wrote: > On Tuesday 24 August 2004 05:11 am, Mauricio Fern�ndez wrote: > > you can make it 'setter-safe' (i.e. go through the normal attribute > > setters instead of bypassing them and assigning to the iv.s directly) > > trivially with Kernel#local_variables. > > clever > > > You'll have to make sure you are introducing new variables (not reusing > > the ones from the outer scope) though, since otherwise there's no way > > to know which locals you used in the with block, short of using > > set_trace_func or doing my_obj.with %{ ..... }. > > > And this won't work with Rite. > > why's that? In Rite, def foo [0].each{ a = 1 } p a #=> 1 end will work, which means that variables introduced in the block will belong to the method, and the only way to create block local vars will be using local{|your,locals,here| ..... } or adding them to the parameter list, so you'd have to write the with construction as follows: foo.with do |x,y| x = 1 y = 2 _ end Otherwise it would work only once in a given scope, cause when running the second instance of whatever.with{...}, the locals introduced in the previous block would already be there and it would be impossible to know what has been assigned in the block, unless you do some ugly hack like assigning some NotSetValue to all those locals before eval'ing the block and then verifying which ones have been set to something else. But this wouldn't be thread safe (if you want several threads within the same method/scope, that is)... > p.s. are we going to get &{ ... } shorthand for lambda? I'd search the RCRs to see if this has been proposed formally in the past, but rcrchive.net seems to be down... -- Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com