From: "Rishel,Wes" Date: 2007-07-01T15:46:03+09:00 Subject: Newbie Question. eval.rb variable scope issue ------_=_NextPart_001_01C7BBAB.7DBF57EF Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable I am a Python duffer who got interested in Rails an decided to check out Ruby. =20 If there is a preferred way to seek help on this please, let me know. =20 Downloaded & installed the Windows version. =20 ruby -v answers ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32] =20 I found eval.rb in the sample library and got some unexpected results, using examples straight from one of the tutorials. =20 Here is the most simplified version I could generate: =20 ruby> word =3D 2 2 ruby> word ERR: undefined local variable or method `word' for main:Object ruby> =20 This seems to be related to the scope of local variables, which is different for each iteration of the main loop in eval.rb. =20 This dandy little Ruby script illustrates the issue =20 a =3D ['word =3D 2', 'word'] a.each do | line | begin print eval(line).inspect, "\n" rescue ScriptError, StandardError print line + "\n" # WJR change printf "ERR: %s\n", $! || 'exception raised' end end print "end of interation\n" =20 begin print eval('word =3D 1').inspect, "\n" rescue ScriptError, StandardError print line + "\n" # WJR change printf "ERR: %s\n", $! || 'exception raised' end print "x\n" begin print eval('word').inspect, "\n" rescue ScriptError, StandardError print line + "\n" # WJR change printf "ERR: %s\n", $! || 'exception raised' end =20 Running it in SciTE gives the following results: =20 >ruby multiEvalCallsA.rb 2 word ERR: undefined local variable or method `word' for main:Object end of interation 1 x 1 >Exit code: 0 =20 Of course if you switch the order, the program runs without an error message, since the non-iterated version establishes the variable 'word' at the outer scope. =20 After some perusing the available on-line manuals I have not been able to find a way to fix eval.rb to remember the scope of variables created in the input to it. Perhaps I am missing something obvious? =20 =20 Wes Rishel Research Vice President Gartner=20 Alameda, CA +1 510 217 3085 Gartner clients: for inquiries contact vertical.industries@gartner.com =20 or phone: +1 203 316 1122 see us at=20 Gartner's Healthcare Summit 2007=20 The Doral Golf Resort, Miami, Florida, November 11-14, 2007 www.healthcareitsummit.com =20 =20 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.=20 ------_=_NextPart_001_01C7BBAB.7DBF57EF--