How do I embed a Ruby interpreter in my C program?

How do I embed a Ruby interpreter in my Win32 C/C++ program? The PicAxe
book is disturbingly laconic on the subject and leaves many seemingly
important details out. Is there a library I can link against that will
contain the interpreter? Do I have to compile the Ruby interpreter
source
code with my project? How does any of this crazy stuff work?
I have been able to find very few resources on the internets
regading
this so links to good sites on the matter will be greatly appreciated.
Thank you…

From: “Just Another Victim of the Ambient M.”
[email protected]

How do I embed a Ruby interpreter in my Win32 C/C++ program? The PicAxe
book is disturbingly laconic on the subject and leaves many seemingly
important details out. Is there a library I can link against that will
contain the interpreter? Do I have to compile the Ruby interpreter source
code with my project? How does any of this crazy stuff work?

Here’s a simple example:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/178389

If you installed using the one-click installer, look in your
ruby/lib directory… you should see:

msvcrt-ruby18-static.lib
msvcrt-ruby18.lib

…and in ruby/bin:

msvcrt-ruby18.dll

So you have your choice as to link statically or dynamically
with the ruby interpreter.

Hope this helps,

Bill

Hello all.
Tell me please, what initialisation order and params are required for
ruby in multithreaded app,
if i do not want to receive this magnificent error:
=============Error=================
SystemStackError: Message = stack level too deep
Backtrace = from D:_BTA_dev\Data\Script/test.rb:7:in `print

Script:
=============test.rb=================
def buildHotkeysList( file1, file2 )

    file1_work= File.new( file1)
    file2_work= File.new( file2 , "w+" )

    file1_work.each_line {|line|
file2_work.print( line )
}

end
buildHotkeysList (file1.txt, file2.txt)

In simple “for ruby” app everything is ok:
=============main.cpp=================
int main( int argc, char **argv )
{
NtInitialize(&argc, &argv);
ruby_init();
ruby_script(“embed”);

  rb_load_protect( rb_str_new2( "Test.rb" ), 0, &status);

ruby_finalize();
ruby_cleanup(0);
}

Platform: Windows
Compiler: Microsoft
Ruby : 1.86
Thread API: ACE thread.

Thank you.

@The OP:

Embedding the Ruby interpreter isn’t that difficult, but there are
tools to make it easier. For example, the Rice library
(http://rice.rubyforge.org) has the VM class that makes it trivially
easy to embed:

Rice::VM *vm = new Rice::VM(argc, argv);

now you’re free to call whatever ruby calls you want, as well as Rice
objects and other wrapping you may need to do. Rice will also do the
necessary cleanup when the VM object is destroyed.

@Ivan:

You can only run the Ruby VM in a single thread.

Jason R.

Yes ‘Rice’ is quite impressive.

But i just want to use ‘ruby_init()’, ‘ruby_finalize()’ at the main
thread.
And ‘rb_load_protect()’ at the other thread.
Is it possible ? And how ?

No, you cannot reliably run Ruby across threads. It’s just not
possible. Pick one thread to house Ruby and keep it there.

Jason

Ivan Gromov wrote:

i just want to use ‘ruby_init()’, ‘ruby_finalize()’ at the main
thread. And ‘rb_load_protect()’ at the other thread.
Is it possible ? And how ?

Yes this is possible. The trick is to embed the execution of Ruby
interpreter inside a POSIX thread[1], while doing the initialization
inside the main thread. See http://www.ruby-forum.com/topic/144747 for
examples and discussion.

In fact, I’ve used this exact technique for my Ruby-VPI project[1],
which embeds Ruby inside a Verilog simulator.

[1] http://ruby-vpi.rubyforge.org

Suraj K. wrote:

Ivan Gromov wrote:

i just want to use ‘ruby_init()’, ‘ruby_finalize()’ at the main
thread. And ‘rb_load_protect()’ at the other thread.
Is it possible ? And how ?

Yes this is possible. The trick is to embed the execution of Ruby
interpreter inside a POSIX thread, while doing the initialization
inside the main thread. See http://www.ruby-forum.com/topic/144747 for
examples and discussion.

Since your target platform is Win32, I’m not sure whether you have the
pthreads library available. So another approach would be to embed the
execution of your Ruby program inside a Ruby thread
(rb_thread_create()). You can synchronize between the thread and your
main C program through the use of Ruby’s thread-safe Queue class.

See http://www.ruby-forum.com/topic/144747#652515 for an example.

Understood. Thank you.

On Thu, May 1, 2008 at 2:40 PM, Jason R. [email protected]
wrote:

objects and other wrapping you may need to do. Rice will also do the
necessary cleanup when the VM object is destroyed.

@Ivan:

You can only run the Ruby VM in a single thread.

Jason R.

Sorry, my Rice example isn’t quite right. From the docs:

  Rice::VM vm(argc, argv);
  vm.run()

will get you up and running.

Jason R

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs