[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to evaluate file as Ruby code?

u235321044

2/22/2006 1:38:00 PM

How do I evaluate a text file as a sequence of Ruby statements?
(For those who happen to know Perl: I would like to have an
equivalent of the Perl 'do FILENAME' feature).

For example, I have a file "foo" which contains the lines

abc=4
def=5

I would like to "evaluate" the file such that after this, the
variable abc is set to 4 and def is set to 5.

I tried the following:

eval(File.new("foo").read)

But after this, abc is still undefined. What am I doing wrong here?

Ronald





--
Sent by mn-pg-p-e-b-consultant-3.com from siemens subpart of com
This is a spam protected message. Please answer with reference header.
Posted via http://www.usenet-re...
7 Answers

Chris Hulan

2/22/2006 2:23:00 PM

0

Ronald Fischer wrote:
> How do I evaluate a text file as a sequence of Ruby statements?
> (For those who happen to know Perl: I would like to have an
> equivalent of the Perl 'do FILENAME' feature).
>
> For example, I have a file "foo" which contains the lines
>
> abc=4
> def=5
>
> I would like to "evaluate" the file such that after this, the
> variable abc is set to 4 and def is set to 5.
>
> I tried the following:
>
> eval(File.new("foo").read)
>
> But after this, abc is still undefined. What am I doing wrong here?
>
> Ronald

require <file> will load and evaluate the file once, but will not load
a file that has already been 'require'd

load <file> will load and evaluate the file, it will reload and
re-evaluate.

Cheers
Chris

Ross Bamford

2/22/2006 2:29:00 PM

0

On Wed, 2006-02-22 at 23:08 +0900, Ronald Fischer wrote:
> How do I evaluate a text file as a sequence of Ruby statements?
> (For those who happen to know Perl: I would like to have an
> equivalent of the Perl 'do FILENAME' feature).
>
> For example, I have a file "foo" which contains the lines
>
> abc=4
> def=5
>
> I would like to "evaluate" the file such that after this, the
> variable abc is set to 4 and def is set to 5.
>
> I tried the following:
>
> eval(File.new("foo").read)
>
> But after this, abc is still undefined. What am I doing wrong here?

It's just the way Ruby works with respect to deciding whether 'a' is a
variable or a method. Your assignment didn't get 'seen' except inside an
eval. You can use eval again to get back there (changed the var names,
def is a keyword):

eval File.read('foo.rb')
p eval('a')
# => 4
p eval('b')
# => 5

But that's a hack, and really I think you should consider an alternate
design - local variables are supposed to be, well, local. Maybe
constants or (though it pains me to say it) globals would be more
suitable?

Another technique I've used with great success is having a class that
acts as context for an external script, providing methods and holding
data in instance variables. You supply a script similar to yours above,
except using instance variables, which is then instance_eval'd in an
instance of that class, after which you have a self-contained
configuration, or whatever else it is you made.

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk



Wybo Dekker

2/22/2006 3:06:00 PM

0

Joel VanderWerf

2/22/2006 7:08:00 PM

0

Ross Bamford wrote:
> Another technique I've used with great success is having a class that
> acts as context for an external script, providing methods and holding
> data in instance variables. You supply a script similar to yours above,
> except using instance variables, which is then instance_eval'd in an
> instance of that class, after which you have a self-contained
> configuration, or whatever else it is you made.

For example, http://raa.ruby-lang.org/proj...

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


u235321044

2/23/2006 12:55:00 PM

0

> load <file> will load and evaluate the file, it will reload and
> re-evaluate.

This is exactly what I'm looking for (and for my problem it is
OK that I revert to local variables).

But now I have a new problem: Exceptions thrown by 'load' can not
be caught!

For example,

begin
load ".defaultpar"
rescue
puts "file not found"
end

If the file does not exist, I get the error message

in `load': no such file to load -- .defaultpar (LoadError)

Of course I can circumvent it by testing before for the existence
of the file, but I wonder how I can find (from the Ruby specification),
which exceptions can be caught by "rescue" and which ones can not.

Ronald







--
Spam protected message from:
Sent by mn-pg-p-e-b-consultant-3.com from siemens in field com
Posted via http://www.usenet-re...

dblack

2/23/2006 1:23:00 PM

0

Harold Hausman

2/23/2006 4:39:00 PM

0

I think rescue by default only catches StandardError (and descendants).

Maybe:

rescue Exception => ex

Will catch this one?

-Harold

On 2/23/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Thu, 23 Feb 2006, Ronald Fischer wrote:
>
> >> load <file> will load and evaluate the file, it will reload and
> >> re-evaluate.
> >
> > This is exactly what I'm looking for (and for my problem it is
> > OK that I revert to local variables).
> >
> > But now I have a new problem: Exceptions thrown by 'load' can not
> > be caught!
> >
> > For example,
> >
> > begin
> > load ".defaultpar"
> > rescue
> > puts "file not found"
> > end
> >
> > If the file does not exist, I get the error message
> >
> > in `load': no such file to load -- .defaultpar (LoadError)
> >
> > Of course I can circumvent it by testing before for the existence
> > of the file, but I wonder how I can find (from the Ruby specification),
> > which exceptions can be caught by "rescue" and which ones can not.
>
> I believe rescue on its own catches RuntimeError and its descendants.
> If you want to rescue something else, you can do:
>
> rescue LoadError
>
>
> David
>
> --
> David A. Black (dblack@wobblini.net)
> Ruby Power and Light (http://www.rubypoweran...)
>
> "Ruby for Rails" chapters now available
> from Manning Early Access Program! http://www.manning.com/b...
>
>