Ruby 3.4.5p51 (2025-07-16 revision 20cda200d3ce092571d0b5d342dadca69636cb0f)
|
Declares rb_define_variable(). More...
#include "ruby/internal/dllexport.h"
#include "ruby/internal/value.h"
#include "ruby/internal/attr/nonnull.h"
#include "ruby/internal/attr/noreturn.h"
Go to the source code of this file.
Typedefs | |
typedef VALUE | rb_gvar_getter_t(ID id, VALUE *data) |
Type that represents a global variable getter function. | |
typedef void | rb_gvar_setter_t(VALUE val, ID id, VALUE *data) |
Type that represents a global variable setter function. | |
typedef void | rb_gvar_marker_t(VALUE *var) |
Type that represents a global variable marker function. |
Functions | |
void | rb_define_variable (const char *name, VALUE *var) |
"Shares" a global variable between Ruby and C. | |
void | rb_define_virtual_variable (const char *name, rb_gvar_getter_t *getter, rb_gvar_setter_t *setter) |
Defines a global variable that is purely function-backended. | |
void | rb_define_hooked_variable (const char *name, VALUE *var, rb_gvar_getter_t *getter, rb_gvar_setter_t *setter) |
Identical to rb_define_virtual_variable(), but can also specify a storage. | |
void | rb_define_readonly_variable (const char *name, const VALUE *var) |
Identical to rb_define_variable(), except it does not allow Ruby programs to assign values to such global variable. | |
void | rb_define_const (VALUE klass, const char *name, VALUE val) |
Defines a Ruby level constant under a namespace. | |
void | rb_define_global_const (const char *name, VALUE val) |
Identical to rb_define_const(), except it defines that of "global", i.e. | |
void | rb_deprecate_constant (VALUE mod, const char *name) |
Asserts that the given constant is deprecated. | |
VALUE | rb_gv_set (const char *name, VALUE val) |
Assigns to a global variable. | |
VALUE | rb_gv_get (const char *name) |
Obtains a global variable. | |
VALUE | rb_iv_get (VALUE obj, const char *name) |
Obtains an instance variable. | |
VALUE | rb_iv_set (VALUE obj, const char *name, VALUE val) |
Assigns to an instance variable. |
Variables | |
rb_gvar_getter_t | rb_gvar_undef_getter |
rb_gvar_setter_t | rb_gvar_undef_setter |
rb_gvar_marker_t | rb_gvar_undef_marker |
rb_gvar_getter_t | rb_gvar_val_getter |
This is the getter function that backs global variables defined from a ruby script. | |
rb_gvar_setter_t | rb_gvar_val_setter |
This is the setter function that backs global variables defined from a ruby script. | |
rb_gvar_marker_t | rb_gvar_val_marker |
This is the setter function that backs global variables defined from a ruby script. | |
rb_gvar_getter_t | rb_gvar_var_getter |
rb_gvar_setter_t | rb_gvar_var_setter |
rb_gvar_marker_t | rb_gvar_var_marker |
rb_gvar_setter_t | rb_gvar_readonly_setter |
This function just raises rb_eNameError. |
Declares rb_define_variable().
Definition in file variable.h.
Type that represents a global variable getter function.
[in] | id | The variable name. |
[in,out] | data | Where the value is stored. |
Definition at line 37 of file variable.h.
typedef void rb_gvar_marker_t(VALUE *var) |
Type that represents a global variable marker function.
[in] | var | Where the value is to be stored. |
Definition at line 53 of file variable.h.
Type that represents a global variable setter function.
[in] | val | The value to set. |
[in] | id | The variable name. |
[in,out] | data | Where the value is to be stored. |
Definition at line 46 of file variable.h.
Defines a Ruby level constant under a namespace.
[out] | klass | Namespace for the constant to reside. |
[in] | name | Name of the constant. |
[in] | val | Value of the constant. |
rb_eTypeError | `klass` is not a kind of rb_cModule. |
rb_eFrozenError | `klass` is frozen. |
Definition at line 3797 of file variable.c.
Referenced by rb_define_const(), and rb_define_global_const().
void rb_define_global_const | ( | const char * | name, |
VALUE | val ) |
Identical to rb_define_const(), except it defines that of "global", i.e.
toplevel constant.
[in] | name | Name of the constant. |
[in] | val | Value of the constant. |
rb_eFrozenError | rb_cObject is frozen. |
Definition at line 3811 of file variable.c.
Referenced by rb_define_global_const(), and ruby_prog_init().
void rb_define_hooked_variable | ( | const char * | name, |
VALUE * | var, | ||
rb_gvar_getter_t * | getter, | ||
rb_gvar_setter_t * | setter ) |
Identical to rb_define_virtual_variable(), but can also specify a storage.
A programmer can use the storage for e.g. memoisation, storing intermediate computation result, etc.
Also you can pass 0 to this function, unlike other variants:
[in] | name | Variable name, in C's string. |
[in] | var | Variable storage. |
[in] | getter | A getter function. |
[in] | setter | A setter function. |
Definition at line 799 of file variable.c.
void rb_define_readonly_variable | ( | const char * | name, |
const VALUE * | var ) |
Identical to rb_define_variable(), except it does not allow Ruby programs to assign values to such global variable.
C codes can still set values at will. This could be handy for you when implementing an errno-like experience, where a method updates a read-only global variable as a side- effect.
[in] | name | Variable (Ruby side). |
[in] | var | Variable (C side). |
Definition at line 824 of file variable.c.
Referenced by rb_define_readonly_variable().
void rb_define_variable | ( | const char * | name, |
VALUE * | var ) |
"Shares" a global variable between Ruby and C.
Normally a Ruby-level global variable is stored somewhere deep inside of the interpreter's execution context, but this way you can explicitly specify its storage.
In the above example a Ruby global variable named $foo is stored in a C global variable named foo.
[in] | name | Variable (Ruby side). |
[in] | var | Variable (C side). |
Definition at line 818 of file variable.c.
Referenced by rb_define_variable().
void rb_define_virtual_variable | ( | const char * | name, |
rb_gvar_getter_t * | getter, | ||
rb_gvar_setter_t * | setter ) |
Defines a global variable that is purely function-backended.
By using this API a programmer can define a global variable that dynamically changes from time to time.
[in] | name | Variable name, in C's string. |
[in] | getter | A getter function. |
[in] | setter | A setter function. |
Definition at line 830 of file variable.c.
void rb_deprecate_constant | ( | VALUE | mod, |
const char * | name ) |
Asserts that the given constant is deprecated.
Attempt to refer such constant will produce a warning.
[in] | mod | Namespace of the target constant. |
[in] | name | Name of the constant. |
rb_eNameError | No such constant. |
rb_eFrozenError | `mod` is frozen. |
Definition at line 3859 of file variable.c.
Referenced by rb_deprecate_constant().
VALUE rb_gv_get | ( | const char * | name | ) |
Obtains a global variable.
[in] | name | Global variable to query. |
RUBY_Qnil | The global variable does not exist. |
otherwise | The value assigned to the global variable. |
Definition at line 1005 of file variable.c.
Referenced by rb_gv_get().
Assigns to a global variable.
[in] | name | Target global variable. |
[in] | val | Value to assign. |
Definition at line 991 of file variable.c.
Referenced by rb_gv_set().
Obtains an instance variable.
[in] | obj | Target object. |
[in] | name | Target instance variable to query. |
rb_eEncodingError | `name` is corrupt (contains Hanzi etc.). |
RUBY_nil | No such instance variable. |
otherwise | The value assigned to the instance variable. |
Definition at line 4284 of file variable.c.
Assigns to an instance variable.
[out] | obj | Target object. |
[in] | name | Target instance variable. |
[in] | val | Value to assign. |
rb_eFrozenError | Can't modify `obj`. |
rb_eArgError | `obj` has too many instance variables. |
Definition at line 4295 of file variable.c.
rb_gvar_setter_t rb_gvar_readonly_setter |
This function just raises rb_eNameError.
Handy when you want to prohibit a global variable from being squashed by someone.
Definition at line 135 of file variable.h.
Referenced by rb_define_readonly_variable(), rb_define_virtual_variable(), and ruby_prog_init().
rb_gvar_getter_t rb_gvar_undef_getter |
This function has no actual usage (than in ruby itself). Please ignore. It was a bad idea to expose this function to 3rd parties, but we can no longer delete it.
Definition at line 62 of file variable.h.
rb_gvar_marker_t rb_gvar_undef_marker |
This function has no actual usage (than in ruby itself). Please ignore. It was a bad idea to expose this function to 3rd parties, but we can no longer delete it.
Definition at line 80 of file variable.h.
rb_gvar_setter_t rb_gvar_undef_setter |
This function has no actual usage (than in ruby itself). Please ignore. It was a bad idea to expose this function to 3rd parties, but we can no longer delete it.
Definition at line 71 of file variable.h.
rb_gvar_getter_t rb_gvar_val_getter |
This is the getter function that backs global variables defined from a ruby script.
Extension libraries can use this if its global variable needs no custom logic.
Definition at line 87 of file variable.h.
Referenced by rb_define_virtual_variable().
rb_gvar_marker_t rb_gvar_val_marker |
This is the setter function that backs global variables defined from a ruby script.
Extension libraries can use this if its global variable needs no custom logic.
Definition at line 101 of file variable.h.
rb_gvar_setter_t rb_gvar_val_setter |
This is the setter function that backs global variables defined from a ruby script.
Extension libraries can use this if its global variable needs no custom logic.
Definition at line 94 of file variable.h.
rb_gvar_getter_t rb_gvar_var_getter |
This function has no actual usage (than in ruby itself). Please ignore. It was a bad idea to expose this function to 3rd parties, but we can no longer delete it.
Definition at line 110 of file variable.h.
Referenced by rb_define_hooked_variable().
rb_gvar_marker_t rb_gvar_var_marker |
This function has no actual usage (than in ruby itself). Please ignore. It was a bad idea to expose this function to 3rd parties, but we can no longer delete it.
Definition at line 128 of file variable.h.
Referenced by rb_define_hooked_variable().
rb_gvar_setter_t rb_gvar_var_setter |
This function has no actual usage (than in ruby itself). Please ignore. It was a bad idea to expose this function to 3rd parties, but we can no longer delete it.
Definition at line 119 of file variable.h.
Referenced by rb_define_hooked_variable().