From: Paul Harrington Date: 2010-09-25T11:58:52+09:00 Subject: Re: Using the same variable for an array and an iterator 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 > 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. -- Posted via http://www.ruby-forum.com/.