Calling C functions from Embedded Ruby Interpreter

M

Mohit Sindhwani

Hi Everyone,

I know the subject line is quite a mouthful! Sorry about that.

I've managed to embed a Ruby interpreter into my application and I can
call Ruby functions from the code in my application. It works fine and
returns results to me as expected. I can also ask it to run a script
for me.

Now, I'd like to get it to do some automation for me. My project starts
up, loads a few things, sets a few properties, etc. I'd like to be able
to automate this using the embedded interpreter. I expect the code in
the script may be as simple as:
set_default_color(0x00ff0000)
set_screen_size(800,600)
...and so on.

Now, as I understand it, in my application, I need to these C functions
in it:
VALUE set_default_color (VALUE color)
VALUE set_screen_size (VALUE w, VALUE h)
... and so on.

I just don't know what to do next! I'm not even sure what I should
search for - should I like at how to create an extension? Does that
mean that I need to get into the mkmf, etc. and compile some things for
it to work (I shudder because my primary environment is Borland C++
Builder/ Turbo C++ and I had enough worries getting the interpreter to
be compiled in).

To use the script from the embedded interpreter, can I just ask it to
run the script?

I'm sorry if some of the questions/ explanations are a bit vague. Any
help/ direction is greatly appreciated.

Thanks
Mohit.
 
S

Simon Krahnke

* Mohit Sindhwani said:
I know the subject line is quite a mouthful! Sorry about that.

I don't see anything wrong in the subject line.
I've managed to embed a Ruby interpreter into my application and I can
call Ruby functions from the code in my application. It works fine and
returns results to me as expected. I can also ask it to run a script
for me.
Grats.

set_default_color(0x00ff0000)
set_screen_size(800,600)
..and so on.

Now, as I understand it, in my application, I need to these C functions
in it:
VALUE set_default_color (VALUE color)
VALUE set_screen_size (VALUE w, VALUE h)
.. and so on.

I just don't know what to do next!

Well, the Pickaxe has a chapter on this stuff.

Try

rb_define_global_function("set_default_color", &set_default_color, 1)

There also is rb_define_module_function(VALUE model, char *name,
VALUE(*func)(), int argc) and rb_define_singleton_method taking the same
arguments. I never tried any of these, and don't quit know how to tell a
module function from a singleton function from a system function. (The
last one is in Kernel, the other two can be in any module or class.)

mfg, simon .... l
 
M

Mohit Sindhwani

Hi Simon

First, my apologies on the late response. Thanks for your email
relating to my request.

Simon said:
I don't see anything wrong in the subject line.
Haha, thanks for that!
Well, the Pickaxe has a chapter on this stuff.

Try

rb_define_global_function("set_default_color", &set_default_color, 1)

There also is rb_define_module_function(VALUE model, char *name,
VALUE(*func)(), int argc) and rb_define_singleton_method taking the same
arguments. I never tried any of these, and don't quit know how to tell a
module function from a singleton function from a system function. (The
last one is in Kernel, the other two can be in any module or class.)

Ya, actually wisdom strikes soon after one requests for help. I did
find the necessary stuff in one of the links that I had been staring at
for a while... and after that, it was easy enough to connect up the dots
to get things moving.

Thanks for the example. I followed the procedure for defining a module
and then creating the functions within it. Not sure if I eventually
want it to be that way but I know enough now to keep exploring.
mfg, simon .... l

Might I ask what "mfg" stands for?

Thanks again.

Cheers,
Mohit.
4/27/2008 | 7:53 PM.
 
M

Mohit Sindhwani

David said:
Hi --



Mit freundlichen Grüßen (with friendly greetings -- more or less like
"Best wishes").


David

Thanks David! That's why I love this list - you learn a little bit
about languages all the time :)

Cheers,
Mohit.
4/27/2008 | 8:32 PM.
 
M

Mohit Sindhwani

Pascal said:
Also, you could improve you google-fu. You'd find nice tools such as:
http://www.silmaril.ie/cgi-bin/uncgi/acronyms?MFG

Point taken, Pascal. Sorry about that.

It was just a friendly question piggy-backed to my response to the
original reply. But, I understand what you mean. I could have avoided
the responses that followed.

By the way, thanks for the link - it's quite neat!

Mfg,
Mohit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top