# Using Ruby as an extension language

**URL:** <https://www.ruby-forum.com/t/using-ruby-as-an-extension-language/85812>\
**Category:** Ruby\
**Created:** [February 16, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/using-ruby-as-an-extension-language/85812 "2007-02-16T15:03:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harold\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@Harold\_H](https://www.ruby-forum.com/u/Harold_H)\
**Post date:** [February 16, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/using-ruby-as-an-extension-language/85812/1 "2007-02-16T15:03:47Z")

</div>

I’d like to use Ruby as an extension language for software written on  
a Windows-like (actually xbox360) platform in C++.

Specifically, I’d like to be able to embed a Ruby interpreter into my  
application so that I can do the following things:

1. Call Ruby functions from C++
2. Call C++ functions from Ruby
3. Exchange data between the C++ application the the Ruby interpreter.

I have experience doing these exact things in Lua. I understand that  
doing this in Ruby is going to be more painful than doing it in Lua as  
this was one of Lua’s primary goals, but what I’m hoping is that it’s  
_possible_ to do it in Ruby. (:

(because we all know that writing Ruby code is so much more enjoyable  
than writing Lua code)

So. I downloaded the Ruby source. I had to hack it up a bit because  
the xbox is slightly different than windows, but I got a red thread  
going. (passing scripts in through -e)

Obviously booting up the interpreter from scratch each time I want to  
run some ruby code isn’t going to work:  
ruby\_init();  
ruby\_options();  
ruby\_run();

Is there a document somewhere that describes how to properly use ruby  
as an extension language? Is the something comparable to luaL\_dofile  
or luaL\_dostring and lua\_register?

Am I way off base?

1000 thanks in advance for any insight,  
-Harold

---

<div class="post-metadata">

**Author:** ![Harold\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@Harold\_H](https://www.ruby-forum.com/u/Harold_H)\
**Post date:** [February 16, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/using-ruby-as-an-extension-language/85812/2 "2007-02-16T15:33:54Z")

</div>

On 16/02/07, Harold H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> doing this in Ruby is going to be more painful than doing it in Lua as  
> Obviously booting up the interpreter from scratch each time I want to
> 
> 1000 thanks in advance for any insight,  
> -Harold

Programming Ruby (Pragmatic Programmers) has a nice section on  
embedding Ruby. Also have a look at README.EXT in the Ruby source  
code.

Farrel

---

<div class="post-metadata">

**Author:** ![Harold\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@Harold\_H](https://www.ruby-forum.com/u/Harold_H)\
**Post date:** [February 16, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/using-ruby-as-an-extension-language/85812/3 "2007-02-16T16:14:12Z")

</div>

Alle venerdì 16 febbraio 2007, Harold H. ha scritto:

> doing this in Ruby is going to be more painful than doing it in Lua as  
> Obviously booting up the interpreter from scratch each time I want to
> 
> 1000 thanks in advance for any insight,  
> -Harold

There’s been a thread on this mailing list about embedding ruby. You may  
be  
interested in it. It’s at  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/235460](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/235460)

I hope this helps

---

<div class="post-metadata">

**Author:** ![Harold\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@Harold\_H](https://www.ruby-forum.com/u/Harold_H)\
**Post date:** [February 17, 2007, 3:47pm UTC](https://www.ruby-forum.com/t/using-ruby-as-an-extension-language/85812/4 "2007-02-17T15:47:35Z")

</div>

On 2/16/07, Farrel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Programming Ruby (Pragmatic Programmers) has a nice section on  
> embedding Ruby. Also have a look at README.EXT in the Ruby source  
> code.

Okay, I’ve read both these, and things are going nicely.  
rb\_eval\_string seems much happier than ruby\_options/ruby\_run… (:

2 questions:

This seems like a nice replacement for luaL\_dofile:  
rb\_eval\_string( “$: \<\< Dir.getwd” );  
rb\_eval\_string( “require ‘.\main.rb’” );

1. This appears to be working nicely, is there anything horribly wrong  
with it?

And my other question comes from when I try to expose a new method to  
the running Ruby. Here are some snippets of my code:  
VALUE rb\_myprint( VALUE inSelf, VALUE inString )  
{  
// snip… but do stuff here, etc…  
return Qnil;  
}

and then later:  
rb\_define\_global\_function( “myprint”, rb\_myprint, 1 );

1. When I try to compile this, I get the following error:  
Error 2 error C2664: ‘rb\_define\_global\_function’ : cannot convert  
parameter 2 from ‘VALUE (\_\_cdecl \*)(VALUE,VALUE)’ to ‘VALUE (\_\_cdecl  
\*)(…)’ d:\code\littlecoder\littlecoder.cpp 68

I’m not familiar with the ‘old-school’ “C” “…” syntax, so I’m kind  
of stuck here. If someone can fill me in on how to properly  
define/declare these function pointers so I can pass them in that’d be  
sweet.

Thanks again,  
-Harold

---

<div class="post-metadata">

**Author:** ![Harold\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@Harold\_H](https://www.ruby-forum.com/u/Harold_H)\
**Post date:** [February 16, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/using-ruby-as-an-extension-language/85812/5 "2007-02-16T16:39:21Z")

</div>

On 2/16/07, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There’s been a thread on this mailing list about embedding ruby. You may be  
> interested in it. It’s at  
> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/235460](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/235460)
> 
> I hope this helps

Thanks to both of you, this is all good reading.

-Harold

---

<div class="post-metadata">

**Author:** ![Harold\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@Harold\_H](https://www.ruby-forum.com/u/Harold_H)\
**Post date:** [February 17, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/using-ruby-as-an-extension-language/85812/6 "2007-02-17T17:07:44Z")

</div>

On 2/17/07, Harold H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. When I try to compile this, I get the following error:  
> Error 2 error C2664: ‘rb\_define\_global\_function’ : cannot convert  
> parameter 2 from ‘VALUE (\_\_cdecl \*)(VALUE,VALUE)’ to ‘VALUE (\_\_cdecl  
> \*)(…)’ d:\code\littlecoder\littlecoder.cpp 68

So, I’ve found _an_ answer to my question, but it feels a little nuts.  
Anyone care to confirm that this is the best practice for exposing C++  
static methods to Ruby?

typedef VALUE (\*HOOK)(…);

///…  
// define static functions, etc…  
///…

rb\_define\_global\_function( “myprint”,  
reinterpret\_cast(rb\_myprint), 1 );

Man, ugly things like reinterpret\_cast make me wish I was working with  
a dynamic language… But that’s the whole point of this exercise to  
begin with, isn’t it? 😛

Many thanks for any input, and apologies for the answering myself noise.

-Harold
