Ruby
3.4.0dev (2024-11-22 revision 8ae7c22972d82b14d7308e16c26389fa4412727f)
|
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. More... | |
void | rb_define_method (VALUE klass, const char *mid, VALUE(*func)(ANYARGS), int arity) |
Defines a method. More... | |
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. More... | |
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. More... | |
void | rb_undef_method (VALUE klass, const char *name) |
Defines an undef of a method. More... | |
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. More... | |
void | rb_define_module_function (VALUE klass, const char *mid, VALUE(*func)(ANYARGS), int arity) |
Defines a module function for a module. More... | |
void | rb_define_global_function (const char *mid, VALUE(*func)(ANYARGS), int arity) |
Defines a global function. More... | |
void | rb_define_alias (VALUE klass, const char *dst, const char *src) |
Defines an alias of a method. More... | |
void | rb_define_attr (VALUE klass, const char *name, int read, int write) |
Defines public accessor method(s) for an attribute. More... | |
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. More... | |
int | rb_get_kwargs (VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values) |
Keyword argument deconstructor. More... | |
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. More... | |
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 . More... | |
int | rb_keyword_given_p (void) |
Determines if the current method is given a keyword argument. More... | |
int | rb_block_given_p (void) |
Determines if the current method is given a block. More... | |
void | rb_need_block (void) |
Declares that the current method needs a block. More... | |
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:
-1
. This means the method body function is "argc and argv" style.-2
. This means the method body function is "self and args" style. 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 916 of file eval.c.
Referenced by rb_ary_delete(), rb_ary_sort_bang(), rb_method_call(), rb_method_call_kw(), and rb_need_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 . |
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. |
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. |
Definition at line 2142 of file class.c.
Referenced by rb_define_singleton_method().
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. |
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. |
Definition at line 2329 of file class.c.
Referenced by rb_define_global_function(), and ruby_prog_init().
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. |
Definition at line 2160 of file class.c.
Referenced by rb_define_module_function().
void rb_define_protected_method | ( | VALUE | klass, |
const char * | mid, | ||
VALUE(*)(ANYARGS) | func, | ||
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. |
Definition at line 2320 of file class.c.
Referenced by rb_define_module_function().
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. |
orighash
points to another hash object, whose contents are the remainder of the operation. 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. |
values
. 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 929 of file eval.c.
Referenced by rb_enumeratorize_with_size().
void rb_need_block | ( | void | ) |
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. |
argv
is filled into the variadic arguments, according to the format. Definition at line 2635 of file class.c.
Referenced by rb_f_trace_var(), rb_f_untrace_var(), and rb_obj_init_clone().
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. |
argv
is filled into the variadic arguments, according to the format. 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. |