c++ - Embed Ruby in C and exntend -
I have a requirement where I need to embed the Ruby code in C ++ (I can get it) and should expand some functionality. The program fails when I need to call my extension module.
I have pasted one of the code below:
main.cpp
#include & lt; Ruby.h & gt; Fixed VALUE total (VALUE rb_self, VALUE rb_param1, VALUE rb_param2) {Double Val 1 = NUM2DBL (rb_param1); Double Val 2 = NUM2 dBL (Rb_prem 2); Return rb_float_new (val1 + val2); } Zero Init_myMod () {myMod = rb_define_module ("myMod"); Rb_define_module_function (myMod, "Yoga", Yoga, 2); } Int main () {... Init Ruby method ... rb_require ("myRubyfile"); Init_myMod (); // This is where I need to increase my Ruby Extension. Rb_funcall (rb_mKernel, rb_intern ("helloworld"), 0, faucet); ....)
myRubyfile.rb
is required "myMod" def helloworld () "Hello World" says #puts "Sum Of .. "+ MyMod.sum (4, 5) End
The problem is that if I use the" myMod "code and print" Hello World ", use Does not, but if I need .., the code dump
this is my requirement where I need to embed a Ruby script in C, and some methods in Ruby (extension) again : Use the.
Can anyone help me with it?
You are receiving a segfault because Ruby VM SEffalts when an exception exception is to wrap the code You need to use something like rb_rescue
or rb_protect
which can increase the exception.
"myMod" is required
is raising an exception because is required
is used only to load files. You are creating modules directly because you do not need anything. So ... why error? Well, if you wanted to write your module in Ruby, you get a syntax error:
module myMod ... end # SyntaxError: Class / module name must be CONSTANT
But when you use rb_define_module
, instead of raising an exception, Ruby does a hard job: this module creates but it's in the Ruby Code Does not display . To open the module in Ruby, simply validate the name:
// .cpp rb_define_module ("MyMod");
and
#. RB is called "Yoga" + MyMod.Sum (4, 5) .to_s