Calling Ruby from C (reverse SWIG)

My C program calls a third-party Windows DLL to provide access to some
specialised hardware. I don’t have source code for this DLL. We’re
losing access to the hardware soon but we want to be able to test our
software via an emulator. I’d like to write the emulator in Ruby.

So I need a simple way to call Ruby from C - a bit like a reverse SWIG.
I can do it using an RPC mechanism to a separate process, but I was
wondering if anybody knew of a simpler way. Something where a Ruby
library ran in the same process as the C code would be ideal.

Any ideas please?

Thanks,
Graham.

On Tue, 2012-11-13 at 09:43 +0900, Graham Menhennitt wrote:

My C program calls a third-party Windows DLL to provide access to some
specialised hardware. I don’t have source code for this DLL. We’re losing access
to the hardware soon but we want to be able to test our software via an emulator.
I’d like to write the emulator in Ruby.

So I need a simple way to call Ruby from C - a bit like a reverse SWIG. I can do
it using an RPC mechanism to a separate process, but I was wondering if anybody
knew of a simpler way. Something where a Ruby library ran in the same process as
the C code would be ideal.

Any ideas please?

Thanks,
Graham.

Programming Ruby: The Pragmatic Programmer's Guide scroll
down to

Embedding a Ruby Interpreter

or google
embedding ruby in C
for other examples

If I am not mistaken, the subtle window manager embeds ruby within its C
code.

On Wed, Nov 14, 2012 at 2:37 AM, Reid T.
[email protected]wrote:

or google
embedding ruby in C
for other examples

Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
email: [email protected]
url: http://www.u-aizu.ac.jp
bib: Web of Science

On 13/11/2012 11:43, Graham Menhennitt wrote:

So I need a simple way to call Ruby from C - a bit like a reverse SWIG. I can do
it using an RPC mechanism to a separate process, but I was wondering if anybody
knew of a simpler way. Something where a Ruby library ran in the
Many thanks to Reid and Saji for replying. I have the embedded Ruby
library working and I’m starting on my emulator.

Thanks again,
Graham

Graham Menhennitt wrote in post #1084538:

Many thanks to Reid and Saji for replying. I have the embedded Ruby
library working and I’m starting on my emulator.

Thanks again,
Graham

That’s great that you got it working. I am trying to accomplish the same
thing. Would you be willing to describe to me how you did it? I am
tearing my hair out trying to figure it out.

Regards,
Justin

Graham Menhennitt wrote in post #1085110:

I’m trying to work out how to get a Windows build going using MSys,
MingW or Visual Studio. If anybody has any hints, it would be
appreciated.

Graham

Yeah, that is what I’m trying to do. So far no luck in Visual Studio. I
was able to get an executable to compile and link with MinGW/MSys, but
when I run the program I get this:

$ ./test_ruby.exe
internal:gem_prelude:1:in require': cannot load such file -- rubygems.rb (LoadError) from <internal:gem_prelude>:1:in

On 19/11/2012 12:29, Justin Workman wrote:

Graham Menhennitt wrote in post #1084538:

Many thanks to Reid and Saji for replying. I have the embedded Ruby
library working and I’m starting on my emulator.
thing. Would you be willing to describe to me how you did it? I am
tearing my hair out trying to figure it out.

It worked pretty much exactly by the book
(http://media.pragprog.com/titles/ruby3/ext_ruby.pdf). I’m building on
Centos5 Linux and I needed to add a few libraries to the link command:
-lpthread -lcrypt -ldl -lrt.

I’m trying to work out how to get a Windows build going using MSys,
MingW or Visual Studio. If anybody has any hints, it would be
appreciated.

Graham