Invoking Ruby code from a low-level language?


#1

Hi, my sincerest apologies if this question has already been answered
somewhere, but google didn’t turn anything up…

I’m working on a small university research project which is trying to
create a network weathermap by creating a large distributed P2P network
of clients installed on ordinary users’ home computers, and collecting
data by running traces etc between the different peers.

We want to use a high-level language and GUI library for the interface,
and FXRuby seems to be a good option for this, but, due to the sort of
computers this client is going to be deployed on (home users,
predominantly Windows, probably not tech-savvy and reluctant to make any
changes to their systems), most of them will probably not have a Ruby
interpreter installed. Rather than forcing people to download Ruby and
Fox and install them themselves, in addition to installing our client,
we would prefer to be able to bundle the Ruby interpreter and GUI
libraries into our application (as dynamic libraries or something) and
then write most of the program in C (the startup code, the
communications layer, etc), and only have the user-interaction stuff
written in Ruby.

So, is it possible to have a C program that invokes Ruby code via an
interpreter library? I’m sure I’ve seen some apps do this with Python,
but I’m not sure if it’s possible with Ruby or not…

Thanks,
Alex Fulton
(Computer Science department, University of Auckland, New Zealand)


#2

Rather than forcing people to download Ruby and
Fox and install them themselves, in addition to installing our client,
we would prefer to be able to bundle the Ruby interpreter and GUI
libraries into our application (as dynamic libraries or something) and
then write most of the program in C (the startup code, the
communications layer, etc), and only have the user-interaction stuff
written in Ruby.

Have a look at http://www.erikveen.dds.nl/rubyscript2exe/

Also you may want to look at Ruby-C
http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html


#3

Have a look at http://www.erikveen.dds.nl/rubyscript2exe/

That looks perfect. Not exactly what I had in mind, but it should serve
our purposes admirably.

Thanks very much!


#4

Alex Fulton wrote:

Have a look at http://www.erikveen.dds.nl/rubyscript2exe/

That looks perfect. Not exactly what I had in mind, but it should serve
our purposes admirably.

Have a look at Shoes. It’s a complete platform built around Ruby, with
both
installers and a package system - Shy. I would compare it favorably to
the old
Amulet GUI for situations like weather maps.


#5

Alex Fulton wrote:

computers this client is going to be deployed on (home users,
So, is it possible to have a C program that invokes Ruby code via an
interpreter library? I’m sure I’ve seen some apps do this with Python,
but I’m not sure if it’s possible with Ruby or not…

Thanks,
Alex Fulton
(Computer Science department, University of Auckland, New Zealand)

see the section “Embedding a Ruby Interpreter” in the pickaxe book and
google
for the same for examples…


#6

On Fri, Dec 12, 2008 at 07:59:39AM +0900, Alex Fulton wrote:

computers this client is going to be deployed on (home users,
So, is it possible to have a C program that invokes Ruby code via an
interpreter library? I’m sure I’ve seen some apps do this with Python,
but I’m not sure if it’s possible with Ruby or not…

The 3 different ways to do this that I know of are:

Of the 3, Shoes has the gui support, and is probably the easiest to get
going
with. Crate may work the best if you have other libraries to compile
against
for your application to work.

Disclaimer: I’m the author of crate.

enjoy,

-jeremy


#7

On Wed, Dec 17, 2008 at 05:22:10AM +0900, Alex Fulton wrote:

I’m interested in a more generic solution.

Any thoughts?

There isn’t anything different to link against for the embedded ruby
interpreter
vs the noral interpreter. Its the same library libruby or
libruby-static
depending on your compilation shared/static needs. The ruby.h that you
found is
probably the ruby.h that you need to look at. On my Mac, the ruby.h is:

mac shipped ruby : /usr/lib/ruby/1.8/universal-darwin9.0/ruby.h
ports ruby : /opt/local/lib/ruby/1.8/i686-darwin9.5.0/ruby.h

One thing you could do to see how to build an application that uses
embedded
ruby is to look at crate. It is has a very thin wrapper around embedded
ruby.

http://github.com/copiousfreetime/crate/tree/master/data/crate_boot.c

Pretty much everything I used to figure out embedded ruby was
http://metaeditor.sourceforge.net/embed/ and the ‘Embedding a Ruby
Interpreter’
section in the Programming Ruby 2nd ed.

hopefully that will help some.

enjoy,

-jeremy


#8

Wow, thanks for the replies everyone. This has given me a lot of
material to get going on (and my boss is now getting irate because I’m
spending too much time researching options and not enough time actually
coding :P).

But I’ve done some research on embedding ruby interpreters (via ruby.h),
and while it seems like this task should be almost trivially easy, I
can’t seem to find a copy of the actual embedded interpreter library and
headers. I did a search on my computer (a Mac) for ruby.h, and it turned
up some platform-specific ruby embedding stuff for making OSX apps, but
I’m interested in a more generic solution.

Any thoughts?


#9

Great, thanks very much! It’s good to hear from another Mac user, as
different platforms seem to have vastly different ways of dealing with
this.


#10

On Wed, Dec 17, 2008 at 06:23:58AM +0900, Alex Fulton wrote:

Great, thanks very much! It’s good to hear from another Mac user, as
different platforms seem to have vastly different ways of dealing with
this.

Easiest/Best way is to just download the ruby tarball and look at the
ruby.h
that ships. That works on every platform :-).

enjoy,

-jeremy


#11

Alex Fulton wrote:

I’m interested in a more generic solution.

Any thoughts?

two simple examples – google for more/more complex examples.

http://my.opera.com/subjam/blog/embedding-ruby-in-c-programs