Take the 2-minute tour ×

Is there any way to pass Ruby objects to a C++ application ? I have never done that kind of thing before and was wondering if that would be possible. Would it require to modify the Ruby core code ?

share|improve this question
When Ruby gets too slow, I think it is being extended with C. –  Hamish Grubijan Jan 13 '10 at 1:36
add comment

4 Answers

up vote 4 down vote accepted

Yes, and no, respectively.

Ruby is written in C. C++ is, by design, C-compatible.

All objects in Ruby are held by a VALUE object (which is a union type), which can be passed around quite easily.

Any directions you find for extending Ruby with C apply in C++ with little modification. Alternatively, you can use something like SWIG to simplify writing your extensions.

share|improve this answer
Pretty much the answer I was looking for. Thanks for helping ! –  Olivier Lalonde Jan 13 '10 at 2:21
add comment

The Programming Ruby book has some details on accessing Ruby from C. I am not sure how current the documentation is though. This blog post describes the Ruby C API as well.

share|improve this answer
add comment

Extending Ruby with C++ is not a problem. Basically the only thing you need to remember when writing your extension is to declare an init method of your extension as extern "C", like so:

extern "C" void Init_your_extension() { // ... }

I recently had to expose a C++ function to my ruby code, you can find the code here if you want (its just basic stuff, so I think it will be good to get you started): http://github.com/psyho/aspell_edit_dist

share|improve this answer
Thanks for sharing. –  Olivier Lalonde Jan 14 '10 at 5:01
add comment

You can build Ruby extensions in C++ using Rice, take a look at http://www.ibm.com/developerworks/library/os-extendruby/

share|improve this answer
add comment

Your Answer


