Block variables

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

With the snipet

x = 10
5.times do |x|
puts “x inside the block: #{x}”
end

puts “x outside the block: #{x}”

and the output is:

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: 10

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 ?

On Tue, Apr 5, 2011 at 5:38 AM, An Nguyen [email protected] wrote:

local to the block."

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. :wink:


Phillip G.

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.

On Tue, Apr 5, 2011 at 8:05 AM, Phillip G.
[email protected] wrote:

On Tue, Apr 5, 2011 at 5:38 AM, An Nguyen [email protected] wrote:

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. :wink:

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™ 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™ Client VM 1.6.0_24) [Windows XP-x86-java]
block 0
block 1
99

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs