Ruby: RString Struct Reference

Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)

Data Fields
RString Struct Reference

Ruby's String. More...

`#include <rstring.h>`

## Data Fields
struct RBasic | basic
| Basic part, including flags and class.
long | len
| Length of the string, not including terminating NUL character.
union { |
struct { |
char * ptr |
| Pointer to the contents of the string. More...
union { |
long capa |
| Capacity of `*ptr`. More...
VALUE shared |
| Parent of the string. More...
} aux |
| Auxiliary info. More...
} heap |
| Strings that use separated memory region for contents use this pattern. More...
struct { |
char ary [1] |
} embed |
| Embedded contents. More...
} | as |
| String's specific fields.

## Detailed Description

Ruby's String.

A string in ruby conceptually has these information:

- Encoding of the string.
- Length of the string.
- Contents of the string.

It is worth noting that a string is not an array of characters in ruby. It has never been. In 1.x a string was an array of integers. Since 2.x a string is no longer an array of anything. A string is a string – just like a Time is not an integer.

Definition at line 196 of file rstring.h.

## Field Documentation

## ◆ ary

char RString::ary[1]

Definition at line 251 of file rstring.h.

## ◆ [union]

union { ... } RString::as

String's specific fields.

Referenced by rb_str_resize().

## ◆ [union]

union { ... } RString::aux

Auxiliary info.

## ◆ basic

struct RBasic RString::basic

Basic part, including flags and class.

Definition at line 199 of file rstring.h.

## ◆ capa

long RString::capa

Capacity of `*ptr`.

A continuous memory region of at least `capa` bytes is expected to exist at `*ptr`. This can be bigger than `len`.

Definition at line 232 of file rstring.h.

## ◆ [struct]

struct { ... } RString::embed

Embedded contents.

## ◆ [struct]

struct { ... } RString::heap

Strings that use separated memory region for contents use this pattern.

## ◆ len

long RString::len

Length of the string, not including terminating NUL character.

Note
  This is in bytes.

Definition at line 206 of file rstring.h.

## ◆ ptr

char* RString::ptr

Pointer to the contents of the string.

In the old days each string had dedicated memory regions. That is no longer true today, but there still are strings of such properties. This field could be used to point such things.

Definition at line 222 of file rstring.h.

## ◆ shared

VALUE RString::shared

Parent of the string.

Nowadays strings can share their contents each other, constructing gigantic nest of objects. This situation is called "shared", and this is the field to control such properties.

Definition at line 240 of file rstring.h.

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

- include/ruby/internal/core/rstring.h