Embeding Ruby in C/C++

I have been reading snippets that people have been saying about Ruby
not being good for embeding, over and over again in different threads.

I have been planning on writing an application in C++ (boo…hiss) and
embeding Ruby inside of it. Basically, I want to write a FAST, RICH
objectory and expose Ruby to it staticly, then allow my end users to
use a pure Ruby API to control it.

My question is: is this doable? I have tried in the past to embed
Ruby and hit a wall, I have been hoping that this would be resolved as
Ruby matured. Is Ruby maturing in the embedable direction?

//ed

I have been reading snippets that people have been saying about Ruby
not being good for embeding, over and over again in different threads.

I have been planning on writing an application in C++ (boo…hiss) and
embeding Ruby inside of it. Basically, I want to write a FAST, RICH
objectory and expose Ruby to it staticly, then allow my end users to
use a pure Ruby API to control it.

Objectory?? I don’t know that word. Does that mean object respository
or something?

My question is: is this doable? I have tried in the past to embed
Ruby and hit a wall, I have been hoping that this would be resolved as
Ruby matured. Is Ruby maturing in the embedable direction?

If you’re wanting to control a C++ app via Ruby, SWIG
might be an option. I’ve never used it personally. I
hear it’s great.

As for Ruby maturing in the direction of embeddability,
my guess is that that will happen, but not soon. Not
with 1.8, possibly with 2.0. Ask Matz.

Hal

···

----- Original Message -----
From: “Edward Wilson” web2ed@yahoo.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, August 20, 2002 3:10 PM
Subject: Embeding Ruby in C/C++

Sure, it’s doable. There are a few issues to consider, though:

  1. You will probably want to avoid or limit the use of threads in your
    Ruby code. They don’t always play well with C++, in my experience.

  2. You need to consider exception-safety. C++ exceptions should never
    escape into Ruby code, and Ruby exceptions should never escape into
    C++ code. If they do, you will have undefined behavior (possibly a
    program crash). Remember that even something as simple as NUM2INT
    can raise an exception. You will find a friend in rb_protect() and
    rb_jump_tag().

  3. If you use gcc 3.x, then use Ruby 1.7, if possible. It has much
    better const-correctness and function declarations (particularly
    functions that take ‘…’).

There are many other minor issues, but these are the ones that show up
often on the mailing list.

Paul

···

On Wed, Aug 21, 2002 at 05:10:50AM +0900, Edward Wilson wrote:

My question is: is this doable? I have tried in the past to embed
Ruby and hit a wall, I have been hoping that this would be resolved as
Ruby matured. Is Ruby maturing in the embedable direction?

In article 017501c24887$48f81d40$0300a8c0@austin.rr.com,

From: “Edward Wilson” web2ed@yahoo.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, August 20, 2002 3:10 PM
Subject: Embeding Ruby in C/C++

I have been reading snippets that people have been saying about Ruby
not being good for embeding, over and over again in different threads.

I have been planning on writing an application in C++ (boo…hiss) and
embeding Ruby inside of it. Basically, I want to write a FAST, RICH
objectory and expose Ruby to it staticly, then allow my end users to
use a pure Ruby API to control it.

Objectory?? I don’t know that word. Does that mean object respository
or something?

My question is: is this doable? I have tried in the past to embed
Ruby and hit a wall, I have been hoping that this would be resolved as
Ruby matured. Is Ruby maturing in the embedable direction?

If you’re wanting to control a C++ app via Ruby, SWIG
might be an option. I’ve never used it personally. I
hear it’s great.

I have used swig some lately and I do highly recommend it. You’ll need to
take a slightly different mindset, though. Instead of embedding Ruby in
your C++ code, you use Ruby to manipulate your C++ objects. This approach
actually works very well and I think offers even greater flexibility.
Search the Ruby talk archives for a thread entitled “embed or swig?” that
I started a few weeks back for more details. Your end users can still
script in Ruby and they’ll have no idea that they’re actually using
objects that are defined in C++ classes - it’ll all look like Ruby to
them.

BTW: Now that Swig 1.3.14 has been released there’s even less stuff you
need to do to make swig generate wrappers for your C++ code - the new
version now automagically supports overloaded methods in C++ (no more
%rename declarations needed in your swig interface files). [Caveat: I
haven’t actually had a chance to try 1.3.14 yet, but it does sound very
cool]

Phil

···

Hal E. Fulton hal9000@hypermetrics.com wrote:

----- Original Message -----