I am a Python duffer who got interested in Rails an decided to check out
Ruby.
If there is a preferred way to seek help on this please, let me know.
Downloaded & installed the Windows version.
ruby -v answers ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
I found eval.rb in the sample library and got some unexpected results,
using examples straight from one of the tutorials.
Here is the most simplified version I could generate:
ruby> word = 2
2
ruby> word
ERR: undefined local variable or method `word’ for main:Object
ruby>
This seems to be related to the scope of local variables, which is
different for each iteration of the main loop in eval.rb.
This dandy little Ruby script illustrates the issue
a = [‘word = 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”
begin
print eval('word = 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
Running it in SciTE gives the following results:
ruby multiEvalCallsA.rb
2
word
ERR: undefined local variable or method `word’ for main:Object
end of interation
1
x
1
Exit code: 0
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.
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?
Wes Rishel
Research Vice President
Gartner
Alameda, CA
+1 510 217 3085
Gartner clients: for inquiries contact
[email protected]
mailto:[email protected]
or phone: +1 203 316 1122
see us at
Gartner’s Healthcare Summit 2007
The Doral Golf Resort, Miami, Florida, November 11-14, 2007
www.healthcareitsummit.com http://www.healthcareitsummit.com/
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.