|
Ruby 4.0.7p0 (2026-09-15 revision 229531a6cfbf07e3caef30dbac24a2a3f3fed482)
|
There are some APIs to define a method from C. More...
Data Structures | |
| struct | rb_scan_args_t |
Functions | |
| void | rb_define_method_id (VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc) |
| Identical to rb_define_method(), except it takes the name of the method in ID instead of C's string. | |
| void | rb_define_method (VALUE klass, const char *mid, VALUE(*func)(ANYARGS), int arity) |
| Defines a method. | |
| void | rb_define_protected_method (VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc) |
| Identical to rb_define_method(), except it defines a protected method. | |
| void | rb_define_private_method (VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc) |
| Identical to rb_define_method(), except it defines a private method. | |
| void | rb_undef_method (VALUE klass, const char *name) |
| Defines an undef of a method. | |
| static enum rb_id_table_iterator_result | undef_method_i (ID name, VALUE value, void *data) |
| void | rb_undef_methods_from (VALUE klass, VALUE super) |
| void | rb_define_singleton_method (VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc) |
| Identical to rb_define_method(), except it defines a singleton method. | |
| void | rb_define_module_function (VALUE klass, const char *mid, VALUE(*func)(ANYARGS), int arity) |
| Defines a module function for a module. | |
| void | rb_define_global_function (const char *mid, VALUE(*func)(ANYARGS), int arity) |
| Defines a global function. | |
| void | rb_define_alias (VALUE klass, const char *dst, const char *src) |
| Defines an alias of a method. | |
| void | rb_define_attr (VALUE klass, const char *name, int read, int write) |
| Defines public accessor method(s) for an attribute. | |
| VALUE | rb_keyword_error_new (const char *error, VALUE keys) |
| static void | rb_keyword_error (const char *error, VALUE keys) |
| static void | unknown_keyword_error (VALUE hash, const ID *table, int keywords) |
| static int | separate_symbol (st_data_t key, st_data_t value, st_data_t arg) |
| VALUE | rb_extract_keywords (VALUE *orighash) |
| Splits a hash into two. | |
| int | rb_get_kwargs (VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values) |
| Keyword argument deconstructor. | |
| static void | rb_scan_args_parse (int kw_flag, const char *fmt, struct rb_scan_args_t *arg) |
| static int | rb_scan_args_assign (const struct rb_scan_args_t *arg, int argc, const VALUE *const argv, va_list vargs) |
| static int | rb_scan_args_result (const struct rb_scan_args_t *const arg, int argc) |
| int | rb_scan_args (int argc, const VALUE *argv, const char *fmt,...) |
| Retrieves argument from argc and argv to given VALUE references according to the format string. | |
| int | rb_scan_args_kw (int kw_flag, int argc, const VALUE *argv, const char *fmt,...) |
| Identical to rb_scan_args(), except it also accepts kw_splat. | |
| int | rb_keyword_given_p (void) |
| Determines if the current method is given a keyword argument. | |
| int | rb_block_given_p (void) |
| Determines if the current method is given a block. | |
| void | rb_need_block (void) |
| Declares that the current method needs a block. | |
There are some APIs to define a method from C.
These API takes a C function as a method body.
Method body functions must return a VALUE and can be one of the following form:
This form is a normal C function, excepting it takes a receiver object as the first argument.
This form takes three parameters: argc, argv and self. self is the receiver. argc is the number of arguments. argv is a pointer to an array of the arguments.
This form takes two parameters: self and args. self is the receiver. args is an Array object which contains the arguments.
Method defining APIs takes the number of parameters which the method will takes. This number is called argc. argc can be:
| int rb_block_given_p | ( | void | ) |
Determines if the current method is given a block.
| false | No block is given. |
| true | A block is given. |
Definition at line 1021 of file eval.c.
Referenced by rb_block_given_p(), rb_method_call(), rb_method_call_kw(), rb_need_block(), and rb_yield_block().
| void rb_define_alias | ( | VALUE | klass, |
| const char * | dst, | ||
| const char * | src ) |
Defines an alias of a method.
| [in,out] | klass | The class which the original method belongs to; this is also where the new method will belong to. |
| [in] | dst | A new name for the method. |
| [in] | src | The original name of the method. |
| rb_eTypeError | klass is a non-module. |
| rb_eFrozenError | klass is frozen. |
| rb_eNameError | There is no such method named as src in klass. |
Definition at line 2860 of file class.c.
Referenced by rb_define_alias().
| void rb_define_attr | ( | VALUE | klass, |
| const char * | name, | ||
| int | read, | ||
| int | write ) |
Defines public accessor method(s) for an attribute.
| [out] | klass | The class which the attribute will belong to. |
| [in] | name | Name of the attribute. |
| [in] | read | Whether to define a getter method. |
| [in] | write | Whether to define a setter method. |
| rb_eTypeError | klass is a non-module. |
| rb_eFrozenError | klass is frozen. |
| rb_eNameError | name invalid as an attr e.g. an operator. |
Definition at line 2866 of file class.c.
Referenced by rb_define_attr().
Defines a global function.
| [in] | mid | Name of the function. |
| [in] | func | The method body. |
| [in] | arity | The number of parameters. See Defining methods. |
Defines a method.
| [out] | klass | A module or a class. |
| [in] | mid | Name of the function. |
| [in] | func | The method body. |
| [in] | arity | The number of parameters. See Defining methods. |
Identical to rb_define_method(), except it takes the name of the method in ID instead of C's string.
| [out] | klass | A module or a class. |
| [in] | mid | Name of the function. |
| [in] | func | The method body. |
| [in] | arity | The number of parameters. See Defining methods. |
| void rb_define_module_function | ( | VALUE | klass, |
| const char * | mid, | ||
| VALUE(* | func )(ANYARGS), | ||
| int | arity ) |
Defines a module function for a module.
| [out] | klass | A module or a class. |
| [in] | mid | Name of the function. |
| [in] | func | The method body. |
| [in] | arity | The number of parameters. See Defining methods. |
Identical to rb_define_method(), except it defines a private method.
| [out] | klass | A module or a class. |
| [in] | mid | Name of the function. |
| [in] | func | The method body. |
| [in] | arity | The number of parameters. See Defining methods. |
| void rb_define_protected_method | ( | VALUE | klass, |
| const char * | mid, | ||
| VALUE(* | func )(ANYARGS), | ||
| int | arity ) |
Identical to rb_define_method(), except it defines a protected method.
| [out] | klass | A module or a class. |
| [in] | mid | Name of the function. |
| [in] | func | The method body. |
| [in] | arity | The number of parameters. See Defining methods. |
Identical to rb_define_method(), except it defines a singleton method.
| [out] | obj | Arbitrary ruby object. |
| [in] | mid | Name of the function. |
| [in] | func | The method body. |
| [in] | arity | The number of parameters. See Defining methods. |
Splits a hash into two.
Takes a hash of various keys, and split it into symbol-keyed parts and others. Symbol-keyed part becomes the return value. What remains are returned as a new hash object stored at the argument pointer.
| [in,out] | orighash | Pointer to a target hash to split. |
Definition at line 2921 of file class.c.
Referenced by rb_extract_keywords().
| int rb_get_kwargs | ( | VALUE | keyword_hash, |
| const ID * | table, | ||
| int | required, | ||
| int | optional, | ||
| VALUE * | values ) |
Keyword argument deconstructor.
Retrieves argument values bound to keywords, which directed by table into values, deleting retrieved entries from keyword_hash along the way. First required number of IDs referred by table are mandatory, and succeeding optional (-optional-1 if optional is negative) number of IDs are optional. If a mandatory key is not contained in keyword_hash, raises rb_eArgError. If an optional key is not present in keyword_hash, the corresponding element in values is set to RUBY_Qundef. If optional is negative, rest of keyword_hash are ignored, otherwise raises rb_eArgError.
| [out] | keyword_hash | Target hash to deconstruct. |
| [in] | table | List of keywords that you are interested in. |
| [in] | required | Number of mandatory keywords. |
| [in] | optional | Number of optional keywords (can be negative). |
| [out] | values | Buffer to be filled. |
| rb_eArgError | Absence of a mandatory keyword. |
| rb_eArgError | Found an unknown keyword. |
Definition at line 2939 of file class.c.
Referenced by rb_get_kwargs().
|
static |
| int rb_keyword_given_p | ( | void | ) |
Determines if the current method is given a keyword argument.
| false | No keyword argument is given. |
| true | Keyword argument(s) are given. |
Definition at line 1034 of file eval.c.
Referenced by rb_enumeratorize_with_size(), rb_keyword_given_p(), and rb_yield_block().
| void rb_need_block | ( | void | ) |
Declares that the current method needs a block.
| rb_eLocalJumpError | No block given. |
Definition at line 1042 of file eval.c.
Referenced by rb_need_block(), and rb_yield_block().
| int rb_scan_args | ( | int | argc, |
| const VALUE * | argv, | ||
| const char * | fmt, | ||
| ... ) |
Retrieves argument from argc and argv to given VALUE references according to the format string.
The format can be described in ABNF as follows:
For example, "12" means that the method requires at least one argument, and at most receives three (1+2) arguments. So, the format string must be followed by three variable references, which are to be assigned to captured arguments. For omitted arguments, variables are set to RUBY_Qnil. NULL can be put in place of a variable reference, which means the corresponding captured argument(s) should be just dropped.
The number of given arguments, excluding an option hash or iterator block, is returned.
| [in] | argc | Length of argv. |
| [in] | argv | Pointer to the arguments to parse. |
| [in] | fmt | Format, in the language described above. |
| [out] | ... | Variables to fill in. |
| rb_eFatal | Malformed fmt. |
| rb_eArgError | Arity mismatch. |
Definition at line 3150 of file class.c.
Referenced by rb_f_trace_var(), rb_f_untrace_var(), rb_obj_init_clone(), and rb_scan_args().
|
static |
| int rb_scan_args_kw | ( | int | kw_splat, |
| int | argc, | ||
| const VALUE * | argv, | ||
| const char * | fmt, | ||
| ... ) |
Identical to rb_scan_args(), except it also accepts kw_splat.
| [in] | kw_splat | How to understand the keyword arguments.
|
| [in] | argc | Length of argv. |
| [in] | argv | Pointer to the arguments to parse. |
| [in] | fmt | Format, in the language described above. |
| [out] | ... | Variables to fill in. |
| rb_eFatal | Malformed fmt. |
| rb_eArgError | Arity mismatch. |
Definition at line 3163 of file class.c.
Referenced by rb_scan_args_kw().
|
static |
|
static |
| void rb_undef_method | ( | VALUE | klass, |
| const char * | name ) |
Defines an undef of a method.
– What?
In ruby, there are two separate concepts called "undef" and "remove_method". The thing you imagine when you "un-define" a method is remove_method. This one on the other hand is masking of a previous method definition. Suppose for instance:
This undef foo at (*1) must not eliminate Foo#foo, because that method is also used from Bar#bar. So instead of physically executing the target method, undef inserts a special filtering entry to the class (Baz this case). That entry, when called, acts as if there were no methods at all. But the original can still be accessible, via ways like Bar#bar above.
| [out] | klass | The class to insert an undef. |
| [in] | name | Name of the undef. |
| rb_eTypeError | klass is a non-module. |
| rb_eFrozenError | klass is frozen. |
Definition at line 2672 of file class.c.
Referenced by rb_undef_method().
|
static |