# Using the same variable for an array and an iterator

**URL:** <https://www.ruby-forum.com/t/using-the-same-variable-for-an-array-and-an-iterator/195556>\
**Category:** Ruby\
**Created:** [September 25, 2010, 4:30am UTC](https://www.ruby-forum.com/t/using-the-same-variable-for-an-array-and-an-iterator/195556 "2010-09-25T04:30:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [September 25, 2010, 4:30am UTC](https://www.ruby-forum.com/t/using-the-same-variable-for-an-array-and-an-iterator/195556/1 "2010-09-25T04:30:50Z")

</div>

I’ve just started learning Ruby and am going through a few tutorials.  
Right now I’m playing around with arrays and am getting a strange  
error. I am creating an array named ‘x’ and creating an iterator  
named x as well. Now I know that this isn’t good practice but I’m  
getting a strange error. The first time I iterate through the entire  
array, simply displaying the contents of the array, I don’t get an  
error at all. If I immediately run the iteration again I get a  
NoMethodError and am trying to figure out why. Here’s a screenshot  
[http://bit.ly/bXZkjp](http://bit.ly/bXZkjp)  
Like I said, I know that this is bad practice, but I’m curious as to  
what is causing this error only after the first go through.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [September 25, 2010, 4:58am UTC](https://www.ruby-forum.com/t/using-the-same-variable-for-an-array-and-an-iterator/195556/2 "2010-09-25T04:58:34Z")

</div>

Ryan wrote:

> I’ve just started learning Ruby and am going through a few tutorials.  
> Right now I’m playing around with arrays and am getting a strange  
> error. I am creating an array named ‘x’ and creating an iterator  
> named x as well. Now I know that this isn’t good practice but I’m  
> getting a strange error. The first time I iterate through the entire  
> array, simply displaying the contents of the array, I don’t get an  
> error at all. If I immediately run the iteration again I get a  
> NoMethodError and am trying to figure out why. Here’s a screenshot  
> [http://bit.ly/bXZkjp](http://bit.ly/bXZkjp)  
> Like I said, I know that this is bad practice, but I’m curious as to  
> what is causing this error only after the first go through.

The error gives a hint as to what’s happening. In Ruby 1.8, variables in  
a block clobber the variables of the outside scope. So your in your  
example, after each is finished iterating, “x” is now 4, even though x  
was defined within that block.

In Ruby 1.9 variables only exist within the scope they’re declared in,  
so that doesn’t have this problem.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [September 25, 2010, 5:01am UTC](https://www.ruby-forum.com/t/using-the-same-variable-for-an-array-and-an-iterator/195556/3 "2010-09-25T05:01:16Z")

</div>

On Sat, Sep 25, 2010 at 11:30 AM, Ryan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> what is causing this error only after the first go through.  
> Ruby 1.8 doesn’t shadow local variables used as block arguments.  
> That means what happens is that x is assigned the current element of  
> the array, until the iteration is finished (x = 4).  
> Then you try to call 4.each, which doesn’t exist.

See for comparison the 1.9 output with warnings enabled:

iota ~ % ruby -vwe ‘x = [1,2,3,4]; x.each{|x| p x }; x.each{|x| p x }’  
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86\_64-linux]  
-e:1: warning: shadowing outer local variable - x  
-e:1: warning: shadowing outer local variable - x  
1  
2  
3  
4  
1  
2  
3  
4

As you can see, 1.9 shadows the outer variable, so the original is not  
changed.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [September 25, 2010, 6:00am UTC](https://www.ruby-forum.com/t/using-the-same-variable-for-an-array-and-an-iterator/195556/4 "2010-09-25T06:00:57Z")

</div>

On Sep 24, 10:55 pm, Michael F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Like I said, I know that this is bad practice, but I’m curious as to  
> > ruby 1.9.2p0 (2010-08-18 revision 29036) [x86\_64-linux]
> 
> As you can see, 1.9 shadows the outer variable, so the original is not changed.
> 
> –  
> Michael F.  
> CTO, The Rubyists, LLC

Ahh, that makes a lot of sense. Thanks guys.

-Ryan
