title: Embed Ruby in C and exntend
body: <p>I have a requirement where I need to embed Ruby code in C++ (I could achieve this) and extend some functionality. When I try to call (require) my extension module, the program fails.</p>
  
  <p>I have pasted a jist of the code below:</p>
  
  <h2>main.cpp</h2>
  
  <pre><code>#include &lt;ruby.h&gt;
  
  static VALUE Sum(VALUE rb_self, VALUE rb_param1, VALUE rb_param2)
  {
      double val1 = NUM2DBL(rb_param1);
      double val2 = NUM2DBL(rb_param2);
      return rb_float_new(val1+val2);
  }
  
  void Init_myMod()
  {
      myMod = rb_define_module("myMod");
      rb_define_module_function(myMod, "Sum", Sum, 2);
  }
  
  int main()
  {
   ... Init ruby method...
  
      rb_require("myRubyfile");
      Init_myMod(); // This is where I need to push my ruby extension.
      rb_funcall(rb_mKernel, rb_intern("helloworld"), 0 , NULL);
  .... 
  }
  </code></pre>
  
  <h2>myRubyfile.rb</h2>
  
  <pre><code>require "myMod"
  
  def helloworld()
      puts "Hello world"
      #puts "Sum of.. " + myMod.sum(4, 5)
  end
  </code></pre>
  
  <p>The problem is if I dont use require "myMod" the code works and prints "Hello World", but if I use require.., the code dumps. </p>
  
  <p>This is my requirement where I need to embed the ruby script in C, and reuse some of the C methods in ruby (extension).</p>
  
  <p>Can someone help me with this? </p>