# Calling C functions from Embedded Ruby Interpreter

**URL:** <https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083>\
**Category:** ruby-talk\
**Created:** [24 April 2008 11:06 UTC](https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083 "2008-04-24T11:06:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani1](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@Mohit\_Sindhwani1](https://rubytalk.org/u/Mohit_Sindhwani1)\
**Post date:** [24 April 2008 11:06 UTC](https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083/1 "2008-04-24T11:06:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani1](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@Mohit\_Sindhwani1](https://rubytalk.org/u/Mohit_Sindhwani1)\
**Post date:** [24 April 2008 11:50 UTC](https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083/2 "2008-04-24T11:50:25Z")

</div>

Hello Again,

I found that the example on this page:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/155482](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/155482)

does have the necessary to help me connect the dots. I was staring at the example for quite a while before I sent the email out, but I guess I just didn't see it.

Thanks anyway.

Cheers,  
Mohit.  
4/24/2008 | 7:48 PM.

Mohit Sindhwani wrote:

> **···**
>
> > 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.

---

<div class="post-metadata">

**Author:** ![Simon\_Krahnke](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Simon\_Krahnke](https://rubytalk.org/u/Simon_Krahnke)\
**Post date:** [24 April 2008 12:35 UTC](https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083/3 "2008-04-24T12:35:04Z")

</div>

\* Mohit Sindhwani \<mo\_mail@onghu.com\> (13:06) schrieb:

> 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

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani1](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@Mohit\_Sindhwani1](https://rubytalk.org/u/Mohit_Sindhwani1)\
**Post date:** [27 April 2008 12:06 UTC](https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083/4 "2008-04-27T12:06:24Z")

</div>

Hi Simon

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

Simon Krahnke wrote:

> \* Mohit Sindhwani \<mo\_mail@onghu.com\> (13:06) schrieb
> 
> > I know the subject line is quite a mouthful! Sorry about that.  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > I don't see anything wrong in the subject line.  
> > &nbsp;&nbsp;

Haha, thanks for 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.  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > 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!  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > 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  
> &nbsp;&nbsp;  
> Might I ask what "mfg" stands for?

Thanks again.

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [27 April 2008 12:29 UTC](https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083/5 "2008-04-27T12:29:51Z")

</div>

Hi --

> **···**
>
> On Sun, 27 Apr 2008, Mohit Sindhwani wrote:
> 
> > Hi Simon
> > 
> > First, my apologies on the late response. Thanks for your email relating to my request.
> > 
> > Simon Krahnke wrote:
> > 
> > > \* Mohit Sindhwani \<mo\_mail@onghu.com\> (13:06) schrieb
> > > 
> > > > I know the subject line is quite a mouthful! Sorry about that.
> > > 
> > > I don't see anything wrong in the subject line.
> > 
> > Haha, thanks for 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.
> > > 
> > > 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.)
> > 
> > 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?
> 
> Mit freundlichen Grüßen (with friendly greetings -- more or less like  
> "Best wishes").
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani1](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@Mohit\_Sindhwani1](https://rubytalk.org/u/Mohit_Sindhwani1)\
**Post date:** [27 April 2008 12:33 UTC](https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083/6 "2008-04-27T12:33:41Z")

</div>

David A. Black wrote:

> Hi --
> 
> > > mfg, simon .... l
> > 
> > Might I ask what "mfg" stands for?
> 
> 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.

---

<div class="post-metadata">

**Author:** ![Pascal\_Bourguignon1](https://avatars.discourse-cdn.com/v4/letter/p/b5ac83/32.png) [@Pascal\_Bourguignon1](https://rubytalk.org/u/Pascal_Bourguignon1)\
**Post date:** [27 April 2008 12:40 UTC](https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083/7 "2008-04-27T12:40:07Z")

</div>

Mohit Sindhwani \<mo\_mail@onghu.com\> writes:

> David A. Black wrote:
> 
> > Hi --
> > 
> > > > mfg, simon .... l
> > > 
> > > Might I ask what "mfg" stands for?
> > 
> > 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 🙂

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

> **···**
>
> --  
> \_\_Pascal Bourguignon\_\_
> 
> > **[Free Software by Pascal Bourguignon](http://informatimago.free.fr/i/)**
> >
> > Pascal Bourguignon's Software

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani1](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@Mohit\_Sindhwani1](https://rubytalk.org/u/Mohit_Sindhwani1)\
**Post date:** [27 April 2008 12:45 UTC](https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083/8 "2008-04-27T12:45:20Z")

</div>

Pascal Bourguignon wrote:

> Also, you could improve you google-fu. You'd find nice tools such as:  
> [http://www.silmaril.ie/cgi-bin/uncgi/acronyms?MFG](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.

---

<div class="post-metadata">

**Author:** ![Arlen\_Christian\_Mar1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Arlen\_Christian\_Mar1](https://rubytalk.org/u/Arlen_Christian_Mar1)\
**Post date:** [27 April 2008 12:58 UTC](https://rubytalk.org/t/calling-c-functions-from-embedded-ruby-interpreter/46083/9 "2008-04-27T12:58:14Z")

</div>

Hi,

> **···**
>
> On Sun, Apr 27, 2008 at 10:40 PM, Pascal Bourguignon \<pjb@triton.local\> wrote:
> 
> > Also, you could improve you google-fu. You'd find nice tools such as:  
> > [http://www.silmaril.ie/cgi-bin/uncgi/acronyms?MFG](http://www.silmaril.ie/cgi-bin/uncgi/acronyms?MFG)
> 
> Hmm, I tried something similar, but I suppose the German answer was one I  
> skipped over subconsciously. We all learn something new!
> 
> Cheers,  
> Arlen.
