From: "Rishel,Wes" Date: 2007-07-02T04:18:45+09:00 Subject: Re: Newbie Question. eval.rb variable scope issue Stefano, mil grazie! I don't know how to pass a suggestion to those who maintain the library, but it would be a one-line change to make the "simple" one use while 1 instead of loop do. -----Original Message----- From: Stefano Crocco [mailto:stefano.crocco@alice.it] Sent: Sunday, July 01, 2007 12:10 PM To: ruby-talk ML 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 This e-mail message, including any attachments, is for the sole use of the person to whom it has been sent, and may contain information that is confidential or legally protected. If you are not the intended recipient or have received this message in error, you are not authorized to copy, distribute, or otherwise use this message or its attachments. Please notify the sender immediately by return e-mail and permanently delete this message and any attachments. Gartner makes no warranty that this e-mail is error or virus free.