views:

135

answers:

3

Hi,

I'm learning Ruby, and I'm starting to play with building extensions in C. I have Programming Ruby The Pragmatic Programmers' Guide and so I can follow that for the basic nuts and bolts. What I was wondering is if there already existed some nifty frameworks/whatever to help interoperability between Ruby and other languages, with C++ being the most important for me. I've tried googling, but the results focus on language comparisons, rather than language interoperability.

TIA,

Andy

+1  A: 

Take a look at SWIG. It's a nice framework for integrating C and C++ programs with other programs written in higher level languages. It was originally written to support Python, TCL, and Perl, but has been expanded to support Ruby as well.

Jordan Lewis
Thanks, this looks very useful.
Andy
PS can't vote your answer up. I find this whole reputation thing really funny - it's like an RPG and XP! Now when do I get magic missiles?
Andy
A: 

If you want to use Ruby with Java, then look into JRuby. It is a Java implementation of Ruby and allows you to call Java libraries/code through Ruby.

http://jruby.org/

Pran
Thanks. I've heard of it, but not looked into it. I imagine I might want to use it for running my Ruby code (need to investigate pros v cons), but I don't think I'd want to do my extensions in Java. It's mainly for performance reasons that I'll be making other language extensions, and I'm most comfortable with C++ anyway...
Andy
A: 

FFI is the recommended way to hook Ruby implementations up to C libraries, but a bit of Googling suggests that this probably won't work as-is with C++, so try SWIG. There's an FFI-SWIG thing here.

Stuart Ellis
Thanks, interesting. From a quick glance it seems that its big advantage would be that I could make C libraries that are not tied to Ruby in any way (no ruby.h, no rb_define_class, etc.) and interface with them using this. Nice, but not exactly what I was looking for in this case. Thanks again.
Andy