From: matz@... (Yukihiro Matsumoto) Date: 2001-05-03T15:03:54+09:00 Subject: [ruby-talk:14582] Re: Arggg Bitten by the block var scope feature!!! Hi, In message "[ruby-talk:14557] Arggg Bitten by the block var scope feature!!!" on 01/05/03, Wayne Scott writes: |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 problem can be avoided in the future version, using <> instead of ||. matz.