Using Ruby as an extension language

H

Harold Hausman

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
 
F

Farrel Lifson

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
 
S

Stefano Crocco

Alle venerd=EC 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=
=20
interested in it. It's at=20
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/235460

I hope this helps
 
H

Harold Hausman

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
 
H

Harold Hausman

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
 

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

No members online now.

Forum statistics

Threads
474,000
Messages
2,570,248
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top