[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using Ruby as an extension language

Harold Hausman

2/16/2007 2:03:00 PM

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

5 Answers

Farrel Lifson

2/16/2007 2:33:00 PM

0

On 16/02/07, Harold Hausman <hhausman@gmail.com> wrote:
> 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
>
>

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

Farrel

Stefano Crocco

2/16/2007 3:14:00 PM

0

Alle venerdì 16 febbraio 2007, Harold Hausman ha scritto:
> 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

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-t...

I hope this helps

Harold Hausman

2/16/2007 3:38:00 PM

0

On 2/16/07, Stefano Crocco <stefano.crocco@alice.it> 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-t...
>
> I hope this helps
>
>

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

-Harold

Harold Hausman

2/17/2007 2:47:00 PM

0

On 2/16/07, Farrel Lifson <farrel.lifson@gmail.com> 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 );

2. 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

Harold Hausman

2/17/2007 4:07:00 PM

0

On 2/17/07, Harold Hausman <hhausman@gmail.com> wrote:
> 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 );
>
> 2. 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<HOOK>(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? :P

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

-Harold