Feature #3731
openEasier Embedding API for Ruby
Description
=begin
With Ruby 1.9, it has become more difficult to embed Ruby in a C application correctly.
It would be nice if there was a clearly documented and simple C API to embed ruby in C programs.
I know Ruby was not designed from the start to be embedded, but Ruby was used before in several
products as an embedded scripting langauge. It should therefore be possible to do so in a
more straightforward way.
Kind Regards,
B.
=end
Updated by asher (Asher Haig) about 14 years ago
What is difficult about this?
Example:
#include <ruby.h>
int main( int argc __attribute__((unused)), char *argv[] __attribute__((unused)) ) {
ruby_init();
// Ruby Options are just like /usr/bin/ruby
// interpreter name, script name, argv ...
char* options[] = { "", "rpdom_test.rb", };
void* node = ruby_options( 2, options );
return ruby_run_node( node );
}
Struck me as potentially a documentation issue, but the actual functionality I think is pretty straightforward?
Asher
Updated by Beoran (Beoran Aegul) about 14 years ago
Dear Asher,
Well, I certainly agree documentation should be improved for this use case! :)
However, the problem with what you suggest is that it doesn't work more than once.
For example; I get a segmentation violation (crash) with a "You may encounter a bug of Ruby interpreter."
on the second call to ruby_run_node();
if I try this on a ruby1.9.1 p0:
#include <ruby.h>
RUBY_GLOBAL_SETUP
int main( int argc __attribute__((unused)), char *argv[] __attribute__((unused)) ) {
RUBY_INIT_STACK
ruby_init();
{
char* options[] = { "", "-e", "puts 'hello'" };
void* node = ruby_options( 2, options );
char* options2[] = { "", "-e", "puts 'world'" };
void* node2 = ruby_options( 2, options2 );
ruby_run_node( node );
ruby_run_node( node2 );
}
}
It may be that this bug is fixed in later versions, but I still have to install them to try.
Another problem is that it's a rather unwieldy API. It's nicer if you can run multiple scripts, or call rb_eval_string multiple times.
It's also nice if you can catch syntax errors in the ruby files loaded and handle them on the C side.
Kind Regards,
B.
Updated by shyouhei (Shyouhei Urabe) about 14 years ago
- Status changed from Open to Feedback
Updated by nahi (Hiroshi Nakamura) over 12 years ago
- Description updated (diff)
- Category set to core
- Status changed from Feedback to Open
- Assignee set to ko1 (Koichi Sasada)
- Target version set to 2.0.0
Updated by shyouhei (Shyouhei Urabe) over 12 years ago
- Status changed from Open to Assigned
Updated by ko1 (Koichi Sasada) over 12 years ago
I'll write document for embedding Ruby on your application.
At first, in Japanese,
Second, translate in English.
Please revise it after I finished.
Updated by ko1 (Koichi Sasada) about 12 years ago
- Priority changed from Normal to 5
Ah, sorry for pending.
I need one more body (brain?) of myself.
Updated by mame (Yusuke Endoh) almost 12 years ago
- Target version changed from 2.0.0 to 2.6
ko1, I think you are not divided yet.
This looks a big feature. I'm setting this to next minor.
Please try it if you have time after you finished other tasks.
--
Yusuke Endoh mame@tsg.ne.jp
Updated by matheusrich (Matheus Richard) 8 months ago
@mame (Yusuke Endoh) @ko1 (Koichi Sasada), is this still needed given that we have mruby?