Rishel,Wes
7/1/2007 3:13:00 PM
Thanks, Bill. It sounds like irb will fill my needs.
I have to say that featuring eval in the tutorials when it doesn't work
for a simple set of interactions like setting a variable and then
printing it seems very newbie unfriendly.
Some languages have a culture that excludes all but the smartest and
those who are willing to devote all their time to memorizing the
nuances. I would put Perl in that category. The initial "affect" of the
Ruby language and community is else-wise, but a couple of things make me
wonder. This includes tutorials that don't work in every case and the
fact that there is no free version of documentation for the current
version, which was released > 3 months ago.
If it weren't for people like you willing to respond it could be very
off-putting indeed.
-----Original Message-----
From: Bill Kelly [mailto:billk@cts.com]
Sent: Sunday, July 01, 2007 12:30 AM
To: ruby-talk ML
Subject: Re: Newbie Question. eval.rb variable scope issue
From: "Rishel,Wes" <Wes.Rishel@gartner.com>
>
> I am a Python duffer who got interested in Rails an decided to check
out
> Ruby.
>
> 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
>
> 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?
Welcome !
I don't know the exact reason, but it seems you'll need to
obtain a binding context from outside the iterator block.
E.g.
a = ['word = 2', 'word']
b = binding
a.each {|line| p( eval(line, b) ) }
Incidentally, have you found `irb` yet? There should be an irb.bat
in the bin directory next to your ruby.exe. IRB is "interactive
ruby", similar to invoking python with no arguments.
A couple tips:
p(expr) is a shortcut for puts(expr.inspect)
So,
print eval(line).inspect, "\n"
can be written as:
p line
Also, puts is like print, but adds the "\n" for you.
So,
print line + "\n"
can be:
puts line
Hope this helps,
Regards,
Bill
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.