From: "Rishel,Wes" Date: 2007-07-02T03:48:31+09:00 Subject: Re: Newbie Question. eval.rb variable scope issue It is reassuring to know that the problem is a local problem. I hope someone will be able to help me understand why it might be happing. Perhaps someone on Windows could try the example and see if it's an issue with the distribution package? In believing that the example failing was consistent with what was written in the same tutorial later on. It states The first assignment you make to a local variable acts something like a declaration. If you refer to an uninitialized local variable, the ruby interpreter thinks of it as an attempt to invoke a method of that name; hence the error message you see above. 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) There are further examples in the tutorial that support my interpretation: ruby> foo = 44; print foo, "\n"; defined? foo 44 "local-variable" ruby> loop{bar=45; print bar, "\n"; break}; defined? bar 45 nil I must be missing something. -----Original Message----- From: Stefano Crocco [mailto:stefano.crocco@alice.it] Sent: Sunday, July 01, 2007 11:39 AM To: ruby-talk ML Subject: Re: Newbie Question. eval.rb variable scope issue Alle domenica 1 luglio 2007, Rishel,Wes ha scritto: > ruby> var = " abc " > " abc " > ruby> "1234#{var}5678" > "1234 abc 5678" > > On my machine eval.rb gives a different result. > > ruby> var = " abc " > " abc " > ruby> "1234#{var}5678" > ERR: undefined local variable or method `var' for > main:Object > ruby> I don't know why you're getting this behaviour. On my system (linux, ruby 1.8.6 p36), the example works correctly: ruby> var = " abc " " abc " ruby> "1234#{var}5678" "1234 abc 5678" > Looking at the scoping rules for Ruby it is hard to understand how > eval.rb was ever able to perform the examples in the tutorial, since the > current output is entirely consistent with the scoping rule. It is also > hard to perceive how one could write a version of eval.rb that would > work, given that it pretty much has to have a loop. Here, I think you're wrong. The Pickaxe first edition states it clearly in the "Expressions" chapter (section "Variable scope and loops"): "The while, until, and for loops are built into the language and do not introduce new scope; previously existing locals can be used in the loop, and any new locals created will be available afterward." This is different from what happens with blocks, where variables created inside the block aren't accessible outside them. Since eval.rb uses a while loop, instead, variables defined in one iteration are kept in the following. 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.