tags:

views:

242

answers:

2

I've successfully created Ruby-C++ bindings in the past using SWIG where the C++ code was compiled as a dynamic library with the Ruby script connecting to it.

However, I'd like to do it the other way around. Create an executable using C++ and enable it to load and execute Ruby code. Ruby should be able to call functions defined on the C++ side as well (naturally, otherwise all I would need is the 'system()' call.)

Does SWIG provide the means to achieve this?

+2  A: 

You may be interested in Ruby embedded into c++

Éric Malenfant
+1  A: 

SWIG solves part of your problem: it lets you create Ruby bindings to C++ functions that you've written. The other half of your problem, making a C++ program that can evaluate Ruby code, can be as easy or complicated as you wish.

The easy way is to divide your program into two halves: a ruby-accessible part that lives in a dll, and an inaccessible part that links with that dll and invokes Ruby via system().

At a slightly higher level, you can use ruby's C API from C++ to run Ruby code. This gives you better control over how Ruby code is executed, and offers you the opportunity to do things like wrap all executed Ruby in blocks that generate C++ exceptions from Ruby exceptions. The downside here is that you will need to understand Ruby's C API, which is not awful but certainly isn't as friendly as, say, Lua's.

If you want more than that, you're into deep waters. Ruby and C++ are both complicated languages, and getting them to interoperate is a real challenge. Éric Malenfant's link may be of some use to you.

David Seiler