From: Stefano Crocco Date: 2007-07-02T04:09:43+09:00 Subject: Re: Newbie Question. eval.rb variable scope issue Alle domenica 1 luglio 2007, Rishel,Wes ha scritto: > It is reassuring to know that the problem is a local problem. It's not a local problem. As I said in my second message, there are two files called eval.rb involved in this problem. One is included in the ruby distribution, is the one you're using and doesn't allow to run the code in the tutorial; the other is the one in the tutorial website (http://www.ruby-doc.org/docs/UsersGuide/rg/eval.txt), which is the one I used, which works correctly. The latter, as the tutorial itself says, is more advanced, but the main difference is that it uses 'while', while the other uses 'loop'. Since 'loop' introduces a new scope, variables defined in one iteration are lost in the next - so the bug. 'while' doesn't introduce a new scope, so the program works. > Generally, the scope of a local variable is one of > > ��������proc{ ... } > ��������*** loop{ ... } *** (my emphasis) > ��������def ... end > ��������class ... end > ��������module ... end > ��������the entire program (unless one of the above applies) You're mostly right here. proc and loop are instance methods of the Kernel module, so they don't create a new scope. What happens is that you pass these methods a block, and it is the block which introduces a new scope (the same happens for every method which accepts a block). I hope this helps Stefano