How to embed Ruby in C++?

2019-01-17 16:22发布

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).

5条回答
女痞
2楼-- · 2019-01-17 16:27

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 :) !

查看更多
Viruses.
3楼-- · 2019-01-17 16:30

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.

查看更多
小情绪 Triste *
4楼-- · 2019-01-17 16:31

Rice is looking very promising.

查看更多
smile是对你的礼貌
5楼-- · 2019-01-17 16:34

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

查看更多
ら.Afraid
6楼-- · 2019-01-17 16:49

You might wish to check out tinyrb.

查看更多
登录 后发表回答