(unknown)

can't you do something evil like this?

~ > cat a.rb
def user_method
   p 42
end

~ > cat b.rb
class Wrapped
   class << self
     module_eval(IO.read('a.rb'))
   end
end
Wrapped.user_method

~ > ruby b.rb
42

b.rb is obviously the C bit.

-a

···

On Wed, 14 Jul 2004, Jesse van den Kieboom wrote:

So how to call a function from the loaded file...

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================

Jesse van den Kieboom wrote:

The loaded file need to call the register_functions, i.e. rather than
define a register_function the file must call any method that it want

Now I'm really confused. I can't call any functions that sit in the
loaded file. I can't define functions from within C that the ruby script
I load can call. What CAN I do then. I need to let my C application know
what methods there are. Every method registered is a little script. And
I need to execute these methods from my C application.

You can't specify a module : the module don't exist after the load this is
why ruby lost the definitions.

So how to call a function from the loaded file...

I'm not sure I've followed the discussion, but this is something I use to load (or require) a script and get an actual object (a module, in fact) that has the methods (and constants) defined in the script:

http://redshift.sourceforge.net/script/

The basic idea is just "module_eval(IO.read(file))", but there's some convenience stuff around that.

> So how to call a function from the loaded file...

Funny thing, cause I got it working now. What I do is I load the script
file using rb_load_file. Then I call ruby_exec. After that I start
defining the classes and global objects/functions the scripts can use.

And with eval_string I can call the functions defined in the script
(such as register_functions and the registered functions themselfs :))

So I hope this is a good way to do this.

Jesse

So I hope this is a good way to do this.

Look in eval.c how is called rb_load_file()

Guy Decoux

Look in eval.c how is called rb_load_file()

I don'treally get your point... But I'm using rb_load now, and its still
working :slight_smile:

Jesse

I don'treally get your point... But I'm using rb_load now, and its still
working :slight_smile:

Look at rb_load_protect() and you don't need ruby_exec() if you use
rb_load()

Guy Decoux

Look at rb_load_protect() and you don't need ruby_exec() if you use
rb_load()

Hmm, rb_load() SEGV's my application. rb_load_file without ruby_exec
will not register functions and do the things I want it to do.
rb_load_file and then ruby_exec will do, so I'm sticking to this.

Jesse

rb_load_file and then ruby_exec will do, so I'm sticking to this.

rb_load_file + ruby_exec is similar to rb_load *except* that many things,
that ruby need, are not done.

See the source of rb_load() (in this function eval_node(self, node) is
similar to your call to ruby_exec())

Guy Decoux