From: Eric Mahurin Date: 2005-09-28T23:34:13+09:00 Subject: Re: creating independent lambdas in loops --- "Ara.T.Howard" wrote: > On Wed, 28 Sep 2005, Eric Mahurin wrote: > > > --- "Kroeger Simon (ext)" > > wrote: > >>> -----Original Message----- > >>> From: Michael Roth [mailto:mroth@nessie.de] > >>> Sent: Wednesday, September 28, 2005 3:12 PM > >>> To: ruby-talk ML > >>> Subject: Re: creating independent lambdas in loops > >>> > >>> Michael Roth wrote: > >>> > >>>> Already tried. Doesn't work... :-\ > >>> > >>> Argh, stop. Looks like it works. But only if 'multi' > >> doesn't appears > >>> somewhere before the each-loop. > >> > >> Completely true. If a variable of that name is already > >> defined it is > >> used (and no local variables are created). > > > > Does anyone else find this "feature" useful? > > > > Is there any way to create a local variable to a block > where > > that variable name is already used? Most other languages > have > > this ability but it looks like ruby doesn't. > > it's everywhere in ruby - it's called the 'block' ;-) > > harp:~ > cat a.rb > def scope *vars; yield *vars; end > > x, y = 0b101010, 0b0 > > p y > > scope(x){|y| p y} > > > harp:~ > ruby a.rb > 0 > 42 Add another "p y" and you'll see y wasn't localized in the block. The block just caused y to be assigned to x. What I'm asking is this: f = nil y = 1 localize { |y| # localize y and call this block y = 2 f = lambda { y } } p y # => 1 p f.call # => 2 y = 3 p y # => 3 p f.call # => 2 Anyway to get this functionality? If block arguments were always made local (regardless of whether that variable was assigned yet or not), we could have done this to be the equivalent of the above localize: lambda { |y| y = 2 f = lambda {y} }.call(nil) ______________________________________________________ Yahoo! for Good Donate to the Hurricane Katrina relief effort. http://store.yahoo.com/redcross-donate3/