I have been using Ruby for several years. Most importantly I have been
this amazing language as a support language for E.V.E. Paradox, a game
have been working on for much of the last three years. Ruby is used
in the tools used to build and test E.V.E. Paradox. It has literally
hundreds of hours. I use Ruby to generate many thousands of lines of C++
from configuration files, automate builds, automatically fix problems
software, generate dynamic web content, manage data, test, maintain the
codebase, and so forth. It is reliable, easy to use, intuitive, and a
base for rapid development of tools. To everyone involved in the
this amazing language, thankyou.
Recently I have upgraded my development machine and with it, all of the
libraries I use with my software. I presently use LUA for embedded
E.V.E. Paradox. After the upgrade I found that several key API calls had
renamed (for no reason AFAICT), and the existing scripts, even when
longer works. I have found LUA generally difficult to work with, and
the basics of the language anyway. The documentation is less than ideal.
started entertaining the idea that it would be very, very nice if I
Ruby as my scripting language instead; basically Ruby is solid and
I have years of experience with it.
This is what I needed to replace LUA:
- Ability to load a script frommemory*, not from a file (important).
- Run any init code in the script.
- Hook a bunch of local functions (C++) so that the script can call them
information from the main code body.
- Call functions in the script both periodically and in response to
an update call every 1/60 of a second, in response to mouse clicks,
- Clean up everything entirely when the level is over.
- Be prepared to run everything again later when the next level needing
scripting is loaded.
- Ideally have separate contexts so that two separate games running at
the same exe) can work in their own scripting environment.
I noticed that Ruby is built with many functions available in a separate
library, which builds easily both under Linux and through MinGW, and has
kind of embeddable interface. I figured there was a chance I could get
going, so I gave myself two days to figure things out and get the basics
The process has been a complete nightmare. I have been plagued by
crashes and uncertain documentation. I have been at this for close to
days. I am using ruby-1.8.5-p12 built from source, LD_LIBRARY_PATH is
other instances of Ruby have been completely removed from the machine.
This is what I have encountered:
- There doesn’t appear to be any way to indicate that Ruby should clean
everything up and get to a state where I can start again. I have
work around this by making each script contain a single unique class
that I use,
but this is a terrible hack.
- How do I load a script from memory? rb_eval_string()?
- The documentation is flawed. It indicates calling rb_run(), but that
eventually call exit, which is the last thing you want from an embedded
language. It mentions rb_apply() as having an argc parameter.
- I am getting inexplicable crashes when calling the garbage collector.
it was my mistake, I eliminated calls repeatedly until I got down to
and two calls to rb_gc(). This crashed. I have searched around and
people with comparable problems, and did the same thing. I have seen
condescending replies with no indication as to why a simple code
as this could cause a crash. Not calling the garbage collector is NOT a
solution, as it could be called outside of my control and I don’t want
randomly crashing when it does.
- I managed to mitigate some crashes by moving the Ruby initialisation
inside a function to near the code initialisation such that the stack
creation will be in place by the time other calls are made. This made a
difference! Is the ruby interpreter performing some operations on the
that I don’t know about? Shouldn’t something like this be mentioned
- I wrapped rb_gc() in a rb_protect() call, didn’t help.
- The documentation is not at all clear how you protect something from
garbage collector, or indicate it is no longer in use.
- How does one call “Foo.new(args)” on a class from outside of Ruby,
args have been generated from outside of Ruby (hence no name) meaning
rv_eval_string is unavailable, and I’m not sure how
be used to call “new” on a class to get an object?
- rb_protect() is a complete mess. Rather than being able to call an
function with a void pointer, you are stuck calling something that takes
single VALUE arg. This means that if you want to call a general
need to resort to tricks like building up a ruby array with the
string, and real args (also in their own array), pass it through, and
deconstruct it on the other end, feeding it into rb_apply() rather than
the more convenient rb_funcall() calls. Writing multiple dedicated
not an option, as I lose the local context (eg. “this”) in C++ when I do
unless I go to the extra effort to wrap it. Is there some reason it is
- The code runs fine for a while if I don’t call the garbage collector.
Unfortunately, I get stack depth errors and eventual segfaults if I make
repeated calls to the Ruby script (via rb_apply()). Since I need to call
“update” function sixty times a second, minimising my call use is not an
- When searching for errors that come up during development (eg. stack
problems, crashes on garbage collection) I get a huge number of hits. I
alone here, and it seems people are having genuine trouble finding a
- I can’t find one example that completely and correctly demonstrates
process involved in having C call Ruby then call C, return values
chain neatly, and clean up. If there was such a reference, it would be
- There seem to be external efforts to clean things up, but they aren’t
it back into the official code. Is the embedded interface not really
be used for anything but extensions to Ruby?
I wonder if Ruby is at all suitable as a general-purpose embedded
language? I am beginning to think that it is not.
The embedded interface and documentation are terrible! I don’t mean any
and I maintain my stance that Ruby is an outstanding language. I love
it. And I
do realise that sometimes code takes some time to mature- I’ve been
since the 90’s and it wasn’t always a smooth ride. Is this the problem,
isn’t quite ready for embedding yet? Or I’m not using the right
Or I’m asking too much? Am I doing the wrong thing?
It probably seems odd for someone with such a high opinion of the
have such a low opinion of the embedded interface. Perhaps it is because
has set my expectations so high that I am that much more easily
Some projects seem to have managed it, and they may work, to some
their use of it seems to be fairly basic, rather than counting on it as
component of the program. VIM has a nice example, although it doesn’t
worry about garbage collection or cleaning up. As such it doesn’t seem
Am I alone in thinking this? Should I be looking at other embeddable
(Python, for example), or just go back and get the LUA code working
again? Am I
doing something wrong?
Is there anything I can do to successfully and reliably add support for
favourite scripting language into my project? Or is it simply an