Ruby Programming/Embedding Ruby

From Wikibooks, open books for an open world
< Ruby Programming
Jump to: navigation, search

Embedding Ruby[edit]

You can embed Ruby in your other C apps, though it is a bit tricky.

http://stackoverflow.com/questions/239315/how-to-embed-ruby-in-c

is a good reference. The most tricky part is I think you can only run it from your apps "main" thread or else it won't do its stack-traversal for garbage collection right. This may not be the case with jruby or Rubinius et al, and possibly might not be the case for 1.9, too (might work there).

Basically you can call any "rb_" method defined in ruby.h after calling init, and they all return you a VALUE object (ruby object).

NB that you can call from ruby into C by writing a C extension. That is the standard way to go.