Block variables

A

An Nguyen

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 ?
 
P

Phillip Gawlowski

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.
 
R

Robert Klemme

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 ?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top