Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

Data Structures | Functions
Defining methods

There are some APIs to define a method from C. These API takes a C function as a method body. More...

## Data Structures
struct | rb_scan_args_t

## Functions
void | rb_define_method_id (VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc)
void | rb_define_method (VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
void | rb_define_protected_method (VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
void | rb_define_private_method (VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
void | rb_undef_method (VALUE klass, const char *name)
void | rb_undef_methods_from (VALUE klass, VALUE super)
void | rb_define_singleton_method (VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
| Defines a singleton method for obj. More...
void | rb_define_module_function (VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
| Defines a module function for module. More...
void | rb_define_global_function (const char *name, VALUE(*func)(ANYARGS), int argc)
| Defines a global function. More...
void | rb_define_alias (VALUE klass, const char *name1, const char *name2)
| Defines an alias of a method. More...
void | rb_define_attr (VALUE klass, const char *name, int read, int write)
| Defines (a) public accessor method(s) for an attribute. More...
MJIT_FUNC_EXPORTED VALUE | rb_keyword_error_new (const char *error, VALUE keys)
| NORETURN (static void rb_keyword_error(const char *error, VALUE keys))
| NORETURN (static void unknown_keyword_error(VALUE hash, const ID *table, int keywords))
VALUE | rb_extract_keywords (VALUE *orighash)
int | rb_get_kwargs (VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
int | rb_scan_args (int argc, const VALUE *argv, const char *fmt,...)
int | rb_scan_args_kw (int kw_flag, int argc, const VALUE *argv, const char *fmt,...)
int | rb_class_has_methods (VALUE c)
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...
ID | rb_frame_this_func (void)
| The original name of the current method. More...
ID | rb_frame_callee (void)
| The name of the current method. More...

## Detailed Description

There are some APIs to define a method from C. These API takes a C function as a method body.

Method body functions
  Method body functions must return a VALUE and can be one of the following form:
  Fixed number of parameters

    This form is a normal C function, excepting it takes a receiver object as the first argument.

    static VALUE my_method(VALUE self, VALUE x, VALUE y);
  argc and argv style

    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.

    static VALUE my_method(int argc, VALUE *argv, VALUE self);
  Ruby array style

    This form takes two parameters: self and args. self is the receiver. args is an Array object which contains the arguments.

    static VALUE my_method(VALUE self, VALUE args);

Number of parameters
  Method defining APIs takes the number of parameters which the method will takes. This number is called argc. argc can be:
  zero or positive number
    This means the method body function takes a fixed number of parameters
  -1
    This means the method body function is "argc and argv" style.
  -2
    This means the method body function is "self and args" style.

## Function Documentation

## ◆ NORETURN() [1/2]

NORETURN | ( | static void | rb_keyword_errorconst char *error, VALUE keys | ) |

## ◆ NORETURN() [2/2]

NORETURN | ( | static void | unknown_keyword_errorVALUE hash, const ID *table, int keywords | ) |

## ◆ rb_block_given_p()

int rb_block_given_p | ( | void |  | ) |

Determines if the current method is given a block.

Return values

  zero | if not given
  non-zero | if given

Definition at line 897 of file eval.c.

References cfp, FALSE, GET_EC, rb_vm_frame_block_handler(), TRUE, and VM_BLOCK_HANDLER_NONE.

Referenced by ossl_pem_passwd_cb(), rb_ary_delete(), rb_method_call(), rb_method_call_kw(), and rb_need_block().

## ◆ rb_class_has_methods()

int rb_class_has_methods | ( | VALUE | c | ) |

Definition at line 2201 of file class.c.

References FALSE, rb_id_table_size(), RCLASS_M_TBL, and TRUE.

## ◆ rb_define_alias()

void rb_define_alias | ( | VALUE | klass,
|  | const char * | name1,
|  | const char * | name2
| ) |  |

Defines an alias of a method.

Parameters

  klass | the class which the original method belongs to
  name1 | a new name for the method
  name2 | the original name of the method

Definition at line 1800 of file class.c.

References klass, rb_alias(), and rb_intern.

## ◆ rb_define_attr()

void rb_define_attr | ( | VALUE | klass,
|  | const char * | name,
|  | int | read,
|  | int | write
| ) |  |

Defines (a) public accessor method(s) for an attribute.

Parameters

  klass | the class which the attribute will belongs to
  name | name of the attribute
  read | a getter method for the attribute will be defined if read is non-zero.
  write | a setter method for the attribute will be defined if write is non-zero.

Definition at line 1813 of file class.c.

References FALSE, klass, name, rb_attr(), rb_intern, read(), and write().

## ◆ rb_define_global_function()

void rb_define_global_function | ( | const char * | name,
|  | VALUE(*)(ANYARGS) | func,
|  | int | argc
| ) |  |

Defines a global function.

Parameters

  name | name of the function
  func | the method body
  argc | the number of parameters, or -1 or -2. see Defining methods.

Definition at line 1787 of file class.c.

References argc, name, rb_define_module_function(), and rb_mKernel.

Referenced by Init_jump(), Init_signal(), Init_vm_eval(), Init_vm_trace(), and InitVM_Random().

## ◆ rb_define_method()

void rb_define_method | ( | VALUE | klass,
|  | const char * | name,
|  | VALUE(*)(ANYARGS) | func,
|  | int | argc
| ) |  |

Definition at line 1551 of file class.c.

References argc, klass, METHOD_VISI_PUBLIC, name, rb_add_method_cfunc(), and rb_intern.

Referenced by Init_Bignum(), Init_Binding(), Init_Comparable(), Init_Enumerable(), Init_eval_method(), Init_ISeq(), Init_nonblock(), Init_socket(), Init_wait(), Init_win32ole_method(), Init_win32ole_variable(), InitVM_console(), InitVM_Enumerator(), rb_define_singleton_method(), rsock_init_ancdata(), rsock_init_ipsocket(), rsock_init_sockifaddr(), rsock_init_sockopt(), rsock_init_sockssocket(), rsock_init_tcpserver(), rsock_init_udpsocket(), rsock_init_unixserver(), rsock_init_unixsocket(), and ruby_Init_Fiber_as_Coroutine().

## ◆ rb_define_method_id()

void rb_define_method_id | ( | VALUE | klass,
|  | ID | mid,
|  | VALUE(*)(ANYARGS) | func,
|  | int | argc
| ) |  |

Definition at line 1542 of file class.c.

References argc, klass, METHOD_VISI_PUBLIC, and rb_add_method_cfunc().

## ◆ rb_define_module_function()

void rb_define_module_function | ( | VALUE | module,
|  | const char * | name,
|  | VALUE(*)(ANYARGS) | func,
|  | int | argc
| ) |  |

Defines a module function for module.

Parameters

  module | an module or a class.
  name | name of the function
  func | the method body
  argc | the number of parameters, or -1 or -2. see Defining methods.

Definition at line 1771 of file class.c.

References argc, name, rb_define_private_method(), and rb_define_singleton_method().

Referenced by exp2(), Init_coverage(), Init_fiddle(), Init_nkf(), Init_object_tracing(), Init_objspace(), Init_objspace_dump(), Init_openssl(), Init_ossl_pkey(), Init_ossl_rand(), and rb_define_global_function().

## ◆ rb_define_private_method()

void rb_define_private_method | ( | VALUE | klass,
|  | const char * | name,
|  | VALUE(*)(ANYARGS) | func,
|  | int | argc
| ) |  |

Definition at line 1569 of file class.c.

References argc, klass, METHOD_VISI_PRIVATE, name, rb_add_method_cfunc(), and rb_intern.

Referenced by Init_cparse(), Init_psych_to_ruby(), Init_psych_yaml_tree(), InitVM_resolv(), rb_define_module_function(), and rsock_init_unixserver().

## ◆ rb_define_protected_method()

void rb_define_protected_method | ( | VALUE | klass,
|  | const char * | name,
|  | VALUE(*)(ANYARGS) | func,
|  | int | argc
| ) |  |

Definition at line 1560 of file class.c.

References argc, klass, METHOD_VISI_PROTECTED, name, rb_add_method_cfunc(), and rb_intern.

## ◆ rb_define_singleton_method()

void rb_define_singleton_method | ( | VALUE | obj,
|  | const char * | name,
|  | VALUE(*)(ANYARGS) | func,
|  | int | argc
| ) |  |

Defines a singleton method for obj.

Parameters

  obj | an arbitrary object
  name | name of the singleton method
  func | the method body
  argc | the number of parameters, or -1 or -2. see Defining methods.

Definition at line 1755 of file class.c.

References rb_define_method().

Referenced by Init_builtin(), Init_Exception(), Init_GC(), Init_ossl_dh(), Init_ossl_dsa(), Init_ossl_engine(), Init_ossl_pkcs12(), Init_ossl_pkcs7(), Init_ossl_rsa(), Init_Proc(), Init_psych(), Init_top_self(), Init_VM(), Init_win32ole_type(), Init_win32ole_typelib(), InitVM_Struct(), rb_define_module_function(), rsock_init_ancdata(), rsock_init_basicsocket(), rsock_init_sockifaddr(), rsock_init_tcpsocket(), and rsock_init_unixsocket().

## ◆ rb_extract_keywords()

VALUE rb_extract_keywords | ( | VALUE * | orighash | ) |

Definition at line 1868 of file class.c.

## ◆ rb_frame_callee()

ID rb_frame_callee | ( | void |  | ) |

The name of the current method.

The function returns the alias if an alias of the method is called. The function can also return 0 if it is not in a method. This case can happen in a toplevel of a source file, for example.

Returns
  the ID of the name or 0.

See also
  rb_frame_this_func

Definition at line 1199 of file eval.c.

Referenced by rb_insecure_operation(), and rb_secure().

## ◆ rb_frame_this_func()

ID rb_frame_this_func | ( | void |  | ) |

The original name of the current method.

The function returns the original name of the method even if an alias of the method is called. The function can also return 0 if it is not in a method. This case can happen in a toplevel of a source file, for example.

Returns
  the ID of the name or 0

See also
  rb_frame_callee

Definition at line 1182 of file eval.c.

Referenced by rb_notimplement().

## ◆ rb_get_kwargs()

int rb_get_kwargs | ( | VALUE | keyword_hash,
|  | const ID * | table,
|  | int | required,
|  | int | optional,
|  | VALUE * | values
| ) |  |

Definition at line 1886 of file class.c.

References extract_kwarg, i, ID2SYM, key, NIL_P, Qnil, rb_ary_push(), and rb_ary_tmp_new().

Referenced by rb_num_get_rounding_option(), and rb_opts_exception_p().

## ◆ rb_keyword_error_new()

MJIT_FUNC_EXPORTED VALUE rb_keyword_error_new | ( | const char * | error,
|  | VALUE | keys
| ) |  |

Definition at line 1819 of file class.c.

References error, i, keys, len, RARRAY_AREF, RARRAY_LEN, rb_eArgError, rb_exc_new_str(), rb_inspect(), rb_sprintf(), rb_str_append(), and rb_str_cat_cstr.

## ◆ rb_need_block()

void rb_need_block | ( | void |  | ) |

Declares that the current method needs a block.

Raises a `LocalJumpError` if not given a block.

Definition at line 931 of file eval.c.

References Qnil, rb_block_given_p(), and rb_vm_localjump_error().

## ◆ rb_scan_args()

int rb_scan_args | ( | int | argc,
|  | const VALUE * | argv,
|  | const char * | fmt,
|  |  | ...
| ) |  |

Definition at line 2159 of file class.c.

References arg, error, and rb_scan_args_t::tmp_buffer.

## ◆ rb_scan_args_kw()

int rb_scan_args_kw | ( | int | kw_flag,
|  | int | argc,
|  | const VALUE * | argv,
|  | const char * | fmt,
|  |  | ...
| ) |  |

Definition at line 2180 of file class.c.

References arg, error, rb_scan_args_t::tmp_buffer, and rb_scan_args_t::vargs.

## ◆ rb_undef_method()

void rb_undef_method | ( | VALUE | klass,
|  | const char * | name
| ) |  |

Definition at line 1575 of file class.c.

References klass, METHOD_VISI_UNDEF, name, rb_add_method(), rb_intern, and VM_METHOD_TYPE_UNDEF.

Referenced by Init_Binding(), Init_VM(), and rsock_init_basicsocket().

## ◆ rb_undef_methods_from()

void rb_undef_methods_from | ( | VALUE | klass,
|  | VALUE | super
| ) |  |

Definition at line 1589 of file class.c.

References rb_id_table_foreach(), and RCLASS_M_TBL.

VALUE
unsigned long VALUE
Definition: ruby.h:102
argv
char ** argv
Definition: ruby.c:223
argc
int argc
Definition: ruby.c:222