On Tue, Apr 5, 2011 at 5:38 AM, An Nguyen <nguyenan169 / gmail.com> wrote:
> Dear all,
>
> I'm new comer in Ruby Forum and Ruby.
>
> 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(main):001:0> RUBY_VERSION
=> "1.9.2"
irb(main):002:0> x = 10
=> 10
irb(main):003:0> 5.times do |x|
irb(main):004:1* puts "x inside block #{x}"
irb(main):005:1> end
x inside block 0
x inside block 1
x inside block 2
x inside block 3
x inside block 4
=> 5
irb(main):006:0> puts "x outside block #{x}"
x outside block 10
=> nil

And:

irb(main):001:0> RUBY_VERSION
=> "1.8.7"
irb(main):002:0> x = 10
=> 10
irb(main):003:0> 5.times do |x|
irb(main):004:1* puts "x inside block #{x}"
irb(main):005:1> end
x inside block 0
x inside block 1
x inside block 2
x inside block 3
x inside block 4
=> 5
irb(main):006:0> puts "x outside block #{x}"
x outside block 4
=> nil

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. ;)

-- 
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.