How to embed Ruby in C++?
Asked Answered
P

5

20

What's the best way to embed Ruby as a scripting language in C++? Using ruby.h? SWIG? Something else? What I need is to expose some C++ objects to Ruby and have the Ruby interpreter evaluate scripts that access these objects. I don't care about extending Ruby or accessing it in C++.

I've found this article on embedding Ruby in C++, and while it's very useful, it's kinda old and I was wondering if there are any other resources on the subject (or open source implementations of similar engines).

Pituri answered 27/10, 2008 at 8:10 Comment(1)
You can use mruby github.com/mruby/mruby now!Onceover
G
5

swig is probablly the way to go..... but ruby doesnt embed too well......

if you want a language that embeds nicely into C++, try lua

Graft answered 27/10, 2008 at 8:49 Comment(4)
I did try lua, it's a wonderful language and was pretty easy to embed. Ruby is a requirement however, as I already have a lot of existing Ruby code I'd like to reuse.Pituri
Yes even don#t try to embedd ruby and use it from a single thread in you multithreaded apps. They (the ruby programs) are changing signal handlers, envionrment variables, current directories etc. all the time.Wendalyn
I realize this answer is half a decade old, but I should mention that Ruby is my favorite language to embed among JS, Lua, and Python. To those that might hesitate, I suggest reading ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html and trying a small example before dismissing it.Fr
I haven't looked at it for a long time, how does it handle multitasking? its big limitation was rubys implementation had a lot of globals meaning if you embed it and use it from multiple threads you get problems, as opposed to lua which was designed specifically to handle that situation.Graft
F
11

Rice is looking very promising.

Frankfurter answered 27/10, 2008 at 16:1 Comment(1)
Note to Windows users: Rice is only known to properly compile and run under Cygwin and Mingw. :(Pituri
S
6

Ruby provides a very helpful README.EXT file. It has lots of information about how to extend Ruby, and convert between C & Ruby types.

There is also this excerpt from the pick axe book which pretty much covers the same thing.

In my case, when I added Ruby scripting to my application I decided against using swig, because my needs were very simple, and I didn't want to add yet another build dependency.

Skillful answered 21/2, 2009 at 20:12 Comment(0)
G
5

swig is probablly the way to go..... but ruby doesnt embed too well......

if you want a language that embeds nicely into C++, try lua

Graft answered 27/10, 2008 at 8:49 Comment(4)
I did try lua, it's a wonderful language and was pretty easy to embed. Ruby is a requirement however, as I already have a lot of existing Ruby code I'd like to reuse.Pituri
Yes even don#t try to embedd ruby and use it from a single thread in you multithreaded apps. They (the ruby programs) are changing signal handlers, envionrment variables, current directories etc. all the time.Wendalyn
I realize this answer is half a decade old, but I should mention that Ruby is my favorite language to embed among JS, Lua, and Python. To those that might hesitate, I suggest reading ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html and trying a small example before dismissing it.Fr
I haven't looked at it for a long time, how does it handle multitasking? its big limitation was rubys implementation had a lot of globals meaning if you embed it and use it from multiple threads you get problems, as opposed to lua which was designed specifically to handle that situation.Graft
E
2

You might wish to check out tinyrb.

Egalitarian answered 21/2, 2009 at 21:15 Comment(0)
M
1

I've been working on Rarity (https://github.com/Plaristote/Rarity), which does two things:

  • Generates Ruby bindings from a YML description of your C++ API
  • Allows interaction with Ruby script in the most C++ fashion there is

I've solved a good deal of question (exception handling, garbage collection)... I haven't seen anywhere else the code generation that Rarity uses to make your bindings come to life. I also haven't seen any other lib that allows such an easy conversion between C++ and Ruby types.

I think Rarity's worth the shot ! And I'd be glad to have some feedback as well :) !

Mise answered 20/8, 2014 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.