From: Chris Moline Date: 2001-05-03T10:46:03+09:00 Subject: [ruby-talk:14567] Re: Arggg Bitten by the block var scope feature!!! On Thu, May 03, 2001 at 08:26:06AM +0900, Wayne Scott wrote: > > I just spend a day debugging a elegant script that had this > function. > > #simplified for this discussion > def fetch(arg2) > # cheesy block to flush cache if it has gotten too big > if @cache.length > 200 > # one pass at flushing cache > @cache.dup.each do |arg2, file| ## (1) > file.flush > @cache.delete arg2 > end > end > unless @cache[arg2] ## (2) > @cache[arg2] = @cl.new(@arg1, arg2) > end > @cache[arg2] > end > > The bug is that at (1) the variable 'arg2' is not local to the block > like I was expecting, but was just a copy of the function parameter > 'arg2'. So when I hit the normal part of the function at (2) the > parameter is trashed if I happened to have executed (1). > > This is a bad feature in a language that I am otherwise falling in > love with. I don't think its really a bad feature( actually I like it cause it comes in handy sometimes. ) I think that you just have to learn that preexisting variables show up in the block. Sincerly, Chris Moline