Hi
I’m reading documentation on
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
Stanislav O. wrote:
puts “x outside the block: #{x}”
BUT in documentation I see that output should be:
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.
Stanislav O. wrote:
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
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>