On 9/5/06, Saumya Dikshit <saumzyster / gmail.com> wrote:
> How can we make C routine call a "method" defined in a ruby script ?
> I am familiar with extending ruby to call C routines with/without swig.
> But I was not able to find a easy solution for it. The only way it may
> workout is spawning the script in a different process/thread from C code and
> then make it do all query/answer sequence by calling a C routine. Is there a
> easy way out?
> Can't we make a C routine call a specific method in a Ruby Script.
>
> Can we access a C stack from Ruby  or vice versa while debugging the Ruby
> extension.

There's some discussion of this on pp 302-303 of the 2nd ed. of the Pickaxe.

The basic idea is to do the following in your C code.

1) include "ruby.h"
2) initialize ruby with:
       ruby_init()
       ruby_init_load_path()
       ruby_script("embedded")
This last sets up the name of a pseudo script file (and $0), and can
be whatever you want.

3) include whatever ruby code you need with rb_require, and/or rb_load_file

4) do what you want using the Ruby C language API.  If at this point
you called ruby_run() you would be handing control over to the ruby
interpreter, which would never return. In your case you don't want to
do that.

5) before you exit your C program call rb_finalize() to clean up the
ruby runtime environment.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/