Ruby 3.5.0dev (2025-07-30 revision e2fbd01714d5f98a7707a47bad6b4759aa132032)
|
#include "ruby/internal/config.h"
#include "ruby/internal/attr/noalias.h"
#include "ruby/internal/attr/nonnull.h"
#include "ruby/internal/attr/pure.h"
#include "ruby/internal/cast.h"
#include "ruby/internal/constant_p.h"
#include "ruby/internal/dllexport.h"
#include "ruby/internal/has/builtin.h"
#include "ruby/internal/value.h"
Go to the source code of this file.
Macros | |
#define | RB_ID2SYM rb_id2sym |
Just another name of rb_id2sym. | |
#define | RB_SYM2ID rb_sym2id |
Just another name of rb_sym2id. | |
#define | ID2SYM RB_ID2SYM |
Old name of RB_ID2SYM. | |
#define | SYM2ID RB_SYM2ID |
Old name of RB_SYM2ID. | |
#define | CONST_ID_CACHE RUBY_CONST_ID_CACHE |
Old name of RUBY_CONST_ID_CACHE. | |
#define | CONST_ID RUBY_CONST_ID |
Old name of RUBY_CONST_ID. | |
#define | RUBY_CONST_ID_CACHE(result, str) |
Old implementation detail of rb_intern(). | |
#define | RUBY_CONST_ID(var, str) |
Returns the cached ID for the given str in var, in compiler independent manner. | |
Functions | |
ID | rb_sym2id (VALUE obj) |
Converts an instance of rb_cSymbol into an ID. | |
VALUE | rb_id2sym (ID id) |
Allocates an instance of rb_cSymbol that has the given id. | |
ID | rb_intern (const char *name) |
Finds or creates a symbol of the given name. | |
ID | rb_intern2 (const char *name, long len) |
Identical to rb_intern(), except it additionally takes the length of the string. | |
ID | rb_intern_str (VALUE str) |
Identical to rb_intern(), except it takes an instance of rb_cString. | |
const char * | rb_id2name (ID id) |
Retrieves the name mapped to the given id. | |
ID | rb_check_id (volatile VALUE *namep) |
Detects if the given name is already interned or not. | |
ID | rb_to_id (VALUE str) |
VALUE | rb_id2str (ID id) |
Identical to rb_id2name(), except it returns a frozen Ruby String instead of a C String. | |
VALUE | rb_sym2str (VALUE symbol) |
Obtain a frozen string representation of a symbol (not including the leading colon). | |
VALUE | rb_to_symbol (VALUE name) |
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary. | |
VALUE | rb_check_symbol (volatile VALUE *namep) |
Identical to rb_check_id(), except it returns an instance of rb_cSymbol instead. | |
static ID | rb_intern_const (const char *str) |
This is a "tiny optimisation" over rb_intern(). | |
Defines rb_intern.
RBIMPL
or rbimpl
are implementation details. Don't take them as canon. They could rapidly appear then vanish. The name (path) of this header file is also an implementation detail. Do not expect it to persist at the place it is now. Developers are free to move it anywhere anytime at will. __VA_ARGS__
is always available. We assume C99 for ruby itself but we don't assume languages of extension libraries. They could be written in C++98. Definition in file symbol.h.
#define RUBY_CONST_ID | ( | var, | |
str | |||
) |
Returns the cached ID for the given str in var, in compiler independent manner.
Use this instead of GCC specific rb_intern() when you want to cache the ID on all platforms certainly.
#define RUBY_CONST_ID_CACHE | ( | result, | |
str | |||
) |
Old implementation detail of rb_intern().
Detects if the given name is already interned or not.
It first tries to convert the argument to an instance of rb_cString if it is neither an instance of rb_cString nor rb_cSymbol. The conversion result is written back to the variable. Then queries if that name was already interned before. If found it returns such id, otherwise zero.
We eventually introduced this API to avoid inadvertent symbol pin-down. Before, there was no way to know if an ID was already interned or not without actually creating one (== leaking memory). By using this API you can avoid such situations:
[in,out] | namep | A pointer to a name to query. |
*namep
must either be an instance of rb_cSymbol, or an instance of rb_cString, or responds to #to_str
method. rb_eTypeError | Can't convert `*namep` into rb_cString. |
rb_eEncodingError | Given string is non-ASCII. |
0 | No such id ever existed in the history. |
otherwise | The id that represents the given name. |
*namep
points to is a converted result object, which is always an instance of either rb_cSymbol or rb_cString. Definition at line 1163 of file symbol.c.
Referenced by rb_check_id(), and rb_f_untrace_var().
Identical to rb_check_id(), except it returns an instance of rb_cSymbol instead.
[in,out] | namep | A pointer to a name to query. |
*namep
must either be an instance of rb_cSymbol, or an instance of rb_cString, or responds to #to_str
method. rb_eTypeError | Can't convert `*namep` into rb_cString. |
rb_eEncodingError | Given string is non-ASCII. |
RUBY_Qnil | No such id ever existed in the history. |
otherwise | The id that represents the given name. |
*namep
points to is a converted result object, which is always an instance of either rb_cSymbol or rb_cString. Definition at line 1220 of file symbol.c.
Referenced by rb_check_symbol().
const char * rb_id2name | ( | ID | id | ) |
Retrieves the name mapped to the given id.
[in] | id | An id to query. |
NULL | Unknown id. |
otherwise | A name that the id represents. |
Identical to rb_id2name(), except it returns a frozen Ruby String instead of a C String.
[in] | id | An id to query. |
RUBY_Qfalse | No such id ever existed in the history. |
otherwise | An instance of rb_cString with the name of id. |
Allocates an instance of rb_cSymbol that has the given id.
[in] | id | An id. |
RUBY_Qfalse | No such id ever existed in the history. |
Otherwise | An allocated rb_cSymbol instance. |
Definition at line 1004 of file symbol.c.
Referenced by rb_id2sym().
ID rb_intern | ( | const char * | name | ) |
Finds or creates a symbol of the given name.
[in] | name | The name of the id. |
rb_eRuntimeError | Too many symbols. |
ID rb_intern2 | ( | const char * | name, |
long | len | ||
) |
Identical to rb_intern(), except it additionally takes the length of the string.
This way you can have a symbol that contains NUL characters.
[in] | name | The name of the id. |
[in] | len | Length of name . |
rb_eRuntimeError | Too many symbols. |
|
inlinestatic |
This is a "tiny optimisation" over rb_intern().
If you pass a string literal, and if your C compiler can special-case strlen of such literal to strength-reduce into an integer constant expression, then this inline function can precalc a part of conversion.
[in] | str | The name of the id. |
rb_eRuntimeError | Too many symbols. |
Definition at line 284 of file symbol.h.
Referenced by Init_class_hierarchy().
Identical to rb_intern(), except it takes an instance of rb_cString.
[in] | str | The name of the id. |
str
must either be an instance of rb_cSymbol, or an instance of rb_cString, or responds to #to_str
method. rb_eTypeError | Can't convert `str` into rb_cString. |
rb_eRuntimeError | Too many symbols. |
Converts an instance of rb_cSymbol into an ID.
[in] | obj | An instance of rb_cSymbol. |
rb_eTypeError | `obj` is not an instance of rb_cSymbol. |
Definition at line 973 of file symbol.c.
Referenced by rb_id_attrset(), rb_intern3(), and rb_sym2id().
Obtain a frozen string representation of a symbol (not including the leading colon).
Done without any object allocations.
[in] | symbol | A rb_cSymbol instance to query. |
symbol
. Definition at line 1023 of file symbol.c.
Referenced by rb_enc_get_index(), rb_gc_latest_gc_info(), rb_gc_stat(), rb_str_format(), rb_sym2str(), and rb_sym_to_s().
Definition at line 12558 of file string.c.
Referenced by rb_f_trace_var().
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
[in] | name | The name of the id. |
name
must either be an instance of rb_cSymbol, or an instance of rb_cString, or responds to #to_str
method. rb_eTypeError | Can't convert `name` into rb_cString. |
rb_eRuntimeError | Too many symbols. |