~ > 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
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:
> 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 :))
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.