From: Robert Klemme Date: 2011-04-05T18:42:55+09:00 Subject: Re: Block variables On Tue, Apr 5, 2011 at 8:05 AM, Phillip Gawlowski wrote: > On Tue, Apr 5, 2011 at 5:38 AM, An Nguyen wrote: >> When I read a page about the *block variables* [followed link] >> >> http://rubylearning.com/satishtalim/ruby_blocks.html >> >> And they say "You will observe that after the block has executed, x >> outside the block is the original x. Hence the block parameter x was >> local to the block." >> >> I tested with my Ruby 1.8.7 >> and the output must be: >> >> # x inside the block: 0 >> # x inside the block: 1 >> # x inside the block: 2 >> # x inside the block: 3 >> # x inside the block: 4 >> # x outside the block: 4 >> >> So the question is here: Why are they different here ? Problem is from >> version or this page has mistake ? IRB test > So, yes, a version mismatch. Please note that 1.9.2 is now the > recommended Ruby version for production, with the 1.8.x series in > maintenance mode (i.e. bug fixes only, no new features). The > tutorial's introduction also recommends to use 1.9. ;) You are right but when it comes to scoping of local variables I would only rely on tests not done with IRB since that tool has some special treatment in that area. Alas, for completeness here's the test: $ allruby -e 'x=99;2.times {|x| puts "block #{x}"}; puts x' CYGWIN_NT-5.1 padrklemme2 1.7.9(0.237/5/3) 2011-03-29 10:10 i686 Cygwin ======================================== ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] block 0 block 1 1 ======================================== ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-cygwin] block 0 block 1 99 ======================================== jruby 1.6.0 (ruby 1.8.7 patchlevel 330) (2011-03-15 f3b6154) (Java HotSpot(TM) Client VM 1.6.0_24) [Windows XP-x86-java] block 0 block 1 1 ======================================== jruby 1.6.0 (ruby 1.9.2 patchlevel 136) (2011-03-15 f3b6154) (Java HotSpot(TM) Client VM 1.6.0_24) [Windows XP-x86-java] block 0 block 1 99 ======================================== Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/