# Documentation mismatch?

**URL:** <https://www.ruby-forum.com/t/documentation-mismatch/189507>\
**Category:** Ruby\
**Created:** [June 2, 2010, 12:53am UTC](https://www.ruby-forum.com/t/documentation-mismatch/189507 "2010-06-02T00:53:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 2, 2010, 12:53am UTC](https://www.ruby-forum.com/t/documentation-mismatch/189507/1 "2010-06-02T00:53:46Z")

</div>

Hi  
I’m reading documentation on

> **[Learn How to Blog and Build Websites for Profit!](https://heyblogging.com)**
>
> Ready to take your blogging skills to the next level? Learn how to build a website that generates income and sets you up for success.

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](http://rubylearning.com) contains  
wrong documentation?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 2, 2010, 1:10am UTC](https://www.ruby-forum.com/t/documentation-mismatch/189507/2 "2010-06-02T01:10:30Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 2, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/documentation-mismatch/189507/3 "2010-06-02T17:29:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stanislavua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@stanislavua](https://www.ruby-forum.com/u/stanislavua)\
**Post date:** [June 2, 2010, 5:53am UTC](https://www.ruby-forum.com/t/documentation-mismatch/189507/4 "2010-06-02T05:53:09Z")

</div>

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\>
