Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)
RTypedData Struct Reference

"Typed" user data. More...

#include <rtypeddata.h>

Data Fields

struct RBasic basic
 The part that all ruby objects have in common.
VALUE fields_obj
 Direct reference to the slots that holds instance variables, if any.
const VALUE type
 This is a const rb_data_type_t *const value, with the low bits set:
void * data
 Pointer to the actual C level struct that you want to wrap.

Detailed Description

"Typed" user data.

By using this, extension libraries can wrap a C struct to make it visible from Ruby. For instance if you have a struct timeval, and you want users to use it,

static inline const rb_data_type_t timeval_type = {
// Note that unspecified fields are 0-filled by default.
.wrap_struct_name = "timeval",
.function = {
.dmark = nullptr, // no need to mark
.dfree = RUBY_TYPED_DEFAULT_FREE, // use ruby_xfree()
.dsize = [](auto) {
return sizeof(struct timeval);
},
},
};
extern "C" void
Init_timeval(void)
{
auto klass = rb_define_class("YourName", rb_cObject);
rb_define_alloc_func(klass, [](auto klass) {
struct timeval *t;
klass, struct timeval, &timeval_type, t);
if (auto i = gettimeofday(t, nullptr); i == -1) {
rb_sys_fail("gettimeofday(3)");
}
else {
return ret;
}
});
}
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1478
VALUE rb_cObject
Object class.
Definition object.c:61
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:80
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:205
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:508

Definition at line 358 of file rtypeddata.h.

Field Documentation

◆ basic

struct RBasic RTypedData::basic

The part that all ruby objects have in common.

Definition at line 361 of file rtypeddata.h.

◆ data

void* RTypedData::data

Pointer to the actual C level struct that you want to wrap.

Definition at line 378 of file rtypeddata.h.

Referenced by rb_data_typed_object_zalloc().

◆ fields_obj

VALUE RTypedData::fields_obj

Direct reference to the slots that holds instance variables, if any.

Definition at line 364 of file rtypeddata.h.

◆ type

const VALUE RTypedData::type

This is a const rb_data_type_t *const value, with the low bits set:

1: Set if object is embedded.

This field stores various information about how Ruby should handle a data. This roughly resembles a Ruby level class (apart from method definition etc.)

Definition at line 375 of file rtypeddata.h.


The documentation for this struct was generated from the following file: