nosferat
nosferat

Reputation: 931

Can I script a C++ application with Ruby like with Lua?

I've just started to read about Ruby, and I was wondering if it can be embedded in a C++ application like Lua which provides a library to parse a given script file: https://stackoverflow.com/a/5127294/399107

Upvotes: 2

Views: 686

Answers (3)

Jörg W Mittag
Jörg W Mittag

Reputation: 369488

Yes, you can. You just need to embed a Ruby engine in your application.

Note that, unlike the main Lua engine, some Ruby engines aren't really that well suited to being embedded into other programs. But, for example, Rubinius, IronRuby and JRuby have been specifically designed with embedding in mind, and even though it isn't pretty, you can embed YARV or MRI as well, even though they are not designed for it.

There's also MRuby, but unlike the others, it doesn't implement the full Ruby Language Specification, it only implements a subset of the ISO Ruby Specification which itself is only a small subset of the intersection of Ruby 1.8 and Ruby 1.9. Plus, it hasn't been released yet, as is evidenced by the fact that not even its homepage exists yet. It is, however, specifically designed for embedding, in both senses of the word: being embedded into other programs, and being useful on an embedded device with very little RAM.

As you may have noticed, it is much easier to embed Ruby into your app if the app is running on the Java platform or the CLI. There are C++ compilers for both the Java platform and the CLI, so that option is not entirely out of the question. Otherwise, I'd say that Rubinius is easier to embed, but more people have tried embedding YARV, so there are more blog posts about how to do that. (Or maybe, embedding Rubinius is so trivial nobody needs to write blog posts about it.)

A long time ago, someone was working on an implementation of Ruby for the Lua VM, but that implementation never went anywhere. Would solve all your problems, though :-)

Upvotes: 2

Torsten Robitzki
Torsten Robitzki

Reputation: 2555

Yes, it's possible. Most of the standard libraries types are written in C. And when you can use C, you can use C++ too. Use extern "C" declared functions to get the right binding. I had a lot of trouble, when using a C++ compiler that was different (different version) from the compiler that was used to compile the ruby interpreter.

Here is the part of the pick axt book, that covers the ruby extension library: http://media.pragprog.com/titles/ruby3/ext_ruby.pdf

In an open source C++ web server project, I wrote a ruby / rack adapter, to use the server with rails: https://github.com/TorstenRobitzki/Sioux/tree/master/source/rack

Upvotes: 0

jbr
jbr

Reputation: 6258

Sure you can. It's possible with with SWIG, or you can make your own bindings for it (or google to see if someone has already done the work). The big question is do you really want to? The ruby interpreter is pretty heavy, and the interface isn't very nice.

Matz is working on an embeddable version of Ruby called mruby, which strives to be as easy to embed and as light as Lua. But its still alpha quality.

Upvotes: 1

Related Questions