Ruby equiv to python execfile?

Hi

What I’m trying to get is to run a standalone .rb file and then get
the variables it’s created so I can pass them into another function.

I think this is trivial and I’m being an idiot.

thx

Ritchie

Hi,

ritchie writes:

What I’m trying to get is to run a standalone .rb file and then get
the variables it’s created so I can pass them into another function.

I think what you want is ‘load file.rb’.

Dan

···


/^Dan Debertin$/
airboss@nodewarrior.org
www.nodewarrior.org
ignorami: n:
The art of folding problem users into representational shapes.

Dan Debertin airboss@nodewarrior.org wrote in message news:15667.20804.862584.245365@senator.nodewarrior.org

Hi,

ritchie writes:

What I’m trying to get is to run a standalone .rb file and then get
the variables it’s created so I can pass them into another function.

I think what you want is ‘load file.rb’.

Dan

if I have test.py

x=1

I can then do

import test
test.x

amd it’ll give me 1.

What’s the simplest way of doing that in Ruby. Do all imports need to
be in modules before they’re picked up?

In my application I’m trying to get to a place where my users can
create scripts that may then be run on demand by the main program. If
I can simplify the syntax as much as possible for those kind of
scripts while still being able to get a handle on any vars/objects
created within them then it’s all good. The following kind of code is
not really a showstopper but it’s not as nice as python.

test.rb …

module Test
def x
10
end
end

then …

load 'test.rb’
include Test
Test.x

One other thing, I want to use eval on the vars from the imported
module, does it automatically have a binding object that I can get
hold of??

Thx Ritchie

ritchie wrote:

Dan Debertin airboss@nodewarrior.org wrote in message news:15667.20804.862584.245365@senator.nodewarrior.org

if I have test.py

x=1

I can then do

import test
test.x

amd it’ll give me 1.

What’s the simplest way of doing that in Ruby. Do all imports need to
be in modules before they’re picked up?

I’ve wished for this from time to time. It would be nice for #load to
return the last value in the loaded file, much like eval does:

eval “1;2;3” # ==> 3

As it stands, the return value of #load is always ‘true’–not very a
useful use of that channel. Using gloabls to work around it is annoying.
Is there a better way or should we RCR?