From: Colin Bartlett Date: 2010-06-02T12:52:44+09:00 Subject: Re: documentation mismatch ? --001636c597a39f001704880405c3 Content-Type: text/plain; charset=ISO-8859-1 This is (I believe) caused by a change in the (implicit) declaration and scope of variables in Ruby 1.9. In 1.9 variables in a block which are "similar" to parameters of methods (that is |x| type variables) are local to that block. But in Ruby before 1.9 such variables are local to a block *unless* they are used/"declared" before the block. I hope the following code run under Ruby_1.8.6 and Ruby_1.9.1 shows the difference. "test-code.rb" x = 355; y = 113 2.times do |x| puts "x inside the block: #{x}" y = 22; z = 7 end puts "x outside the block: #{x}" puts "y outside the block: #{y}" puts "z outside the block: #{z}" rescue puts $!.inspect x = 355; y = 113 2.times do |z| puts "z inside the block: #{z}" x = 33; y = 22 end puts "x outside the block: #{x}" puts "y outside the block: #{y}" puts "z outside the block: #{z}" rescue puts $!.inspect ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32] x inside the block: 0 x inside the block: 1 x outside the block: 1 y outside the block: 22 # z inside the block: 0 z inside the block: 1 x outside the block: 33 y outside the block: 22 # ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32] running with warnings on gives: test-code.rb:2 warning: shadowing outer local variable - x x inside the block: 0 x inside the block: 1 x outside the block: 355 y outside the block: 22 # z inside the block: 0 z inside the block: 1 x outside the block: 33 y outside the block: 22 # --001636c597a39f001704880405c3--