Skip to content

Overview

This section explains how to wrap C++ code for use in Ruby. Rice provides a simple, intuitive API that lets you expose C++ classes, methods, functions, and data to Ruby with minimal boilerplate.

Core Concepts

Wrapping C++ code involves several key concepts:

Classes and Modules

Use define_class<T>() to wrap a C++ class as a Ruby class, or define_module() to create a Ruby module for organizing functions. See Constructors for details on defining classes.

Methods and Functions

Use define_method() to wrap C++ member functions as Ruby instance methods, and define_function() or define_singleton_function() for static/class methods. See Methods for details.

Attributes

Use define_attr() to expose C++ member variables as Ruby attributes with getter/setter methods. See Attributes for details.

Type Conversion

Rice automatically converts between C++ and Ruby types. For fundamental types (int, float, string, etc.), values are copied. For wrapped C++ classes, objects are shared between the two languages. See Type Conversion for details.

Quick Reference

Task Rice API
Wrap a C++ class define_class<MyClass>("MyClass")
Add a constructor .define_constructor(Constructor<MyClass, Args...>())
Wrap a member function .define_method("name", &MyClass::method)
Wrap a static function .define_singleton_function("name", &MyClass::static_method)
Expose a member variable .define_attr("name", &MyClass::member)
Wrap an enum define_enum<MyEnum>("MyEnum")
Add a constant .define_constant("NAME", value)

Topics