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:
- Call Ruby functions from C++
- Call C++ functions from Ruby
- 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
On 16/02/07, Harold H. [email protected] 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
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
I hope this helps
On 2/16/07, Farrel L. [email protected] 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’” );
- 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 );
- 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
On 2/16/07, Stefano C. [email protected] 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
I hope this helps
Thanks to both of you, this is all good reading.
-Harold
On 2/17/07, Harold H. [email protected] wrote:
- 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