documentation mismatch ?

  • Thread starter Stanislav Orlenko
  • Start date
S

Stanislav Orlenko

Hi
I'm reading documentation on
http://rubylearning.com/satishtalim/ruby_blocks.html
I've just tried execute this code:

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

puts "x outside the block: #{x}"

and my output:

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

BUT in documentation I see that output should 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: 10

My ruby:
#ruby --version
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]

Can someone clarify the situation? http://rubylearning.com contains
wrong documentation?
Thanks in advance
 
M

Michael W Ryder

Stanislav said:
Hi
I'm reading documentation on
http://rubylearning.com/satishtalim/ruby_blocks.html
I've just tried execute this code:

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

puts "x outside the block: #{x}"

and my output:

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

BUT in documentation I see that output should 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: 10

My ruby:
#ruby --version
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]

Can someone clarify the situation? http://rubylearning.com contains
wrong documentation?
Thanks in advance

I get the results that the documentation shows. I am using ruby
1.9.1p129 (2009-05-12 revision 23412) [i386-mswin32] if that makes any
difference.
 
C

Colin Bartlett

[Note: parts of this message were removed to make it a legal post.]

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
#<NameError: undefined local variable or method `z' for main:Object>
z inside the block: 0
z inside the block: 1
x outside the block: 33
y outside the block: 22
#<NameError: undefined local variable or method `z' for main:Object>

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
#<NameError: undefined local variable or method `z' for main:Object>
z inside the block: 0
z inside the block: 1
x outside the block: 33
y outside the block: 22
#<NameError: undefined local variable or method `z' for main:Object>
 
B

Brian Candler

Stanislav said:
x = 10
5.times do |x|
puts "x inside the block: #{x}"
end

In ruby 1.9, the |x| block parameter is different to the x variable
declared outside. You'll also get a warning if you use the -w flag.

$ ruby19 -w try.rb
try.rb:2: warning: shadowing outer local variable - x
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
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,191
Latest member
BuyKetoBeez

Latest Threads

Top