Defining methods

There are some APIs to define a method from C. More...

## 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_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...
int | rb_obj_basic_to_s_p (VALUE obj)
int | rb_scan_args (int argc, const VALUE *argv, const char *fmt,...)

## 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

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 1381 of file class.c.

References rb_alias(), and rb_intern.

Referenced by Init_Array(), Init_Hash(), Init_IO(), Init_nkf(), Init_ossl_asn1(), Init_ossl_bn(), Init_ossl_dh(), Init_ossl_digest(), Init_ossl_dsa(), Init_ossl_hmac(), Init_ossl_ns_spki(), Init_ossl_pkcs7(), Init_ossl_rsa(), Init_ossl_ssl(), Init_ossl_x509cert(), Init_ossl_x509crl(), Init_ossl_x509name(), Init_ossl_x509req(), Init_process(), Init_Struct(), and Init_win32ole().

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 1394 of file class.c.

References FALSE, rb_attr(), and rb_intern.

Referenced by Init_syck(), and Init_zlib().

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 1368 of file class.c.

References func, rb_define_module_function(), and rb_mKernel.

Referenced by Init_bigdecimal(), Init_Binding(), Init_Complex(), Init_eval(), Init_Exception(), Init_File(), Init_IO(), Init_jump(), Init_load(), Init_Object(), Init_pathname(), Init_Proc(), Init_process(), Init_Random(), Init_Rational(), Init_signal(), Init_Thread(), Init_vm_eval(), process_options(), and ruby_Init_Continuation_body().

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

Definition at line 1206 of file class.c.

References func, NOEX_PUBLIC, rb_add_method_cfunc(), and rb_intern.

Referenced by Init_Array(), Init_bigdecimal(), Init_Bignum(), Init_Binding(), Init_bubblebabble(), Init_Comparable(), Init_Complex(), Init_Cont(), Init_cstr(), Init_curses(), Init_date_core(), Init_dbm(), Init_digest(), Init_Dir(), Init_dlcfunc(), Init_dlhandle(), Init_dlptr(), Init_ellipsize(), Init_enc_associate(), Init_Encoding(), Init_Enumerable(), Init_Enumerator(), Init_eval(), Init_eval_method(), Init_Exception(), Init_fiddle_closure(), Init_fiddle_function(), Init_File(), Init_GC(), Init_gdbm(), Init_Hash(), Init_iconv(), Init_IO(), Init_ISeq(), Init_load(), Init_modify(), Init_nonblock(), Init_Numeric(), Init_numhash(), Init_Object(), Init_ossl_asn1(), Init_ossl_bn(), Init_ossl_cipher(), Init_ossl_dh(), Init_ossl_digest(), Init_ossl_dsa(), Init_ossl_hmac(), Init_ossl_ns_spki(), Init_ossl_pkcs12(), Init_ossl_pkcs7(), Init_ossl_pkey(), Init_ossl_rsa(), Init_ossl_ssl(), Init_ossl_ssl_session(), Init_ossl_x509attr(), Init_ossl_x509cert(), Init_ossl_x509crl(), Init_ossl_x509ext(), Init_ossl_x509name(), Init_ossl_x509req(), Init_ossl_x509revoked(), Init_ossl_x509store(), Init_pack(), Init_parser(), Init_pathname(), Init_Proc(), Init_process(), Init_psych_emitter(), Init_psych_parser(), Init_pty(), Init_Random(), Init_Range(), Init_Rational(), Init_Regexp(), Init_resize(), Init_sdbm(), Init_set_len(), Init_signal(), Init_socket(), Init_String(), Init_stringio(), Init_strscan(), Init_Struct(), Init_syck(), Init_tcltklib(), Init_Thread(), Init_Time(), Init_tkutil(), Init_transcode(), Init_vm_eval(), Init_wait(), Init_win32ole(), Init_zlib(), InitVM_console(), rb_define_singleton_method(), rsock_init_addrinfo(), rsock_init_ancdata(), rsock_init_basicsocket(), rsock_init_ipsocket(), rsock_init_sockopt(), rsock_init_sockssocket(), rsock_init_tcpserver(), rsock_init_tcpsocket(), rsock_init_udpsocket(), rsock_init_unixserver(), rsock_init_unixsocket(), ruby_Init_Continuation_body(), and ruby_Init_Fiber_as_Coroutine().

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

Definition at line 1200 of file class.c.

References func, NOEX_PUBLIC, and rb_add_method_cfunc().

Referenced by Init_VM(), and make_struct().

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 1354 of file class.c.

References func, rb_define_private_method(), and rb_define_singleton_method().

Referenced by define_filetest_function(), exp1(), Init_bubblebabble(), Init_bug(), Init_coverage(), Init_curses(), Init_digest(), Init_dl(), Init_dlcfunc(), Init_dln(), Init_etc(), Init_GC(), Init_marshal(), Init_nkf(), Init_objspace(), Init_openssl(), Init_ossl_asn1(), Init_ossl_cipher(), Init_ossl_pkcs5(), Init_ossl_pkey(), Init_process(), Init_pty(), Init_readline(), Init_signal(), Init_syck(), Init_syslog(), Init_tcltklib(), Init_zlib(), and rb_define_global_function().

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

Definition at line 1218 of file class.c.

References func, NOEX_PRIVATE, rb_add_method_cfunc(), and rb_intern.

Referenced by Init_Complex(), Init_cparse(), Init_date_core(), Init_digest(), Init_eval(), Init_eval_method(), Init_Object(), Init_ossl_digest(), Init_ossl_ssl(), Init_Proc(), Init_psych_to_ruby(), Init_psych_yaml_tree(), Init_Random(), Init_Rational(), Init_strscan(), Init_vm_eval(), and rb_define_module_function().

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

Definition at line 1212 of file class.c.

References func, NOEX_PROTECTED, rb_add_method_cfunc(), and rb_intern.

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 1339 of file class.c.

References func, rb_define_method(), and singleton_class_of().

Referenced by define_filetest_function(), Init_Array(), Init_bigdecimal(), Init_bubblebabble(), Init_Complex(), Init_Cont(), Init_date_core(), Init_dbm(), Init_digest(), Init_Dir(), Init_dlhandle(), Init_dlptr(), Init_Encoding(), Init_etc(), Init_eval(), Init_eval_method(), Init_Exception(), Init_File(), Init_funcall(), Init_GC(), Init_gdbm(), Init_Hash(), Init_iconv(), Init_IO(), Init_ISeq(), Init_old_thread_select(), Init_ossl_asn1(), Init_ossl_bn(), Init_ossl_dh(), Init_ossl_dsa(), Init_ossl_hmac(), Init_ossl_pkcs12(), Init_ossl_pkcs7(), Init_ossl_rsa(), Init_pathname(), Init_Proc(), Init_process(), Init_psych(), Init_pty(), Init_Random(), Init_readline(), Init_Regexp(), Init_sdbm(), Init_socket(), Init_String(), Init_stringio(), Init_strscan(), Init_Struct(), Init_syck(), Init_Thread(), Init_Time(), Init_tkutil(), Init_top_self(), Init_transcode(), Init_VM(), Init_wait_for_single_fd(), Init_win32ole(), Init_zlib(), InitVM_console(), ip_get_encoding_table(), make_struct(), rb_define_module_function(), rb_detach_process(), rb_gzreader_initialize(), rb_gzwriter_initialize(), rsock_init_addrinfo(), rsock_init_ancdata(), rsock_init_basicsocket(), rsock_init_ipsocket(), rsock_init_sockopt(), rsock_init_tcpsocket(), rsock_init_unixsocket(), and ruby_Init_Fiber_as_Coroutine().

int rb_obj_basic_to_s_p | ( | VALUE | obj | ) |

Definition at line 1400 of file class.c.

References rb_method_definition_struct::body, rb_method_definition_struct::cfunc, CLASS_OF, rb_method_entry_struct::def, rb_method_cfunc_struct::func, rb_any_to_s(), rb_intern, rb_method_entry(), rb_method_definition_struct::type, and VM_METHOD_TYPE_CFUNC.

Referenced by rb_obj_inspect().

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

Definition at line 1412 of file class.c.

References argc, hash(), i, ISDIGIT, last, NIL_P, p, Qnil, rb_ary_new(), rb_ary_new4(), rb_block_given_p(), rb_block_proc(), rb_check_convert_type(), rb_eArgError, rb_fatal(), rb_raise(), and T_HASH.

Referenced by addrinfo_getnameinfo(), addrinfo_initialize(), addrinfo_s_getaddrinfo(), argf_getpartial(), argf_read(), ary_take_first_or_last(), BigDecimal_ceil(), BigDecimal_div2(), BigDecimal_dump(), BigDecimal_floor(), BigDecimal_limit(), BigDecimal_mode(), BigDecimal_new(), BigDecimal_power(), BigDecimal_round(), BigDecimal_to_s(), BigDecimal_truncate(), bind_eval(), bsock_setsockopt(), bsock_shutdown(), cbsubst_table_setup(), class_instance_method_list(), count_nodes(), count_objects(), count_objects_size(), count_tdata_objects(), cParser_initialize(), d_lite_new_offset(), d_lite_new_start(), d_lite_next_day(), d_lite_next_month(), d_lite_next_year(), d_lite_prev_day(), d_lite_prev_month(), d_lite_prev_year(), d_lite_step(), date_s__parse_internal(), date_s__strptime_internal(), date_s_civil(), date_s_commercial(), date_s_httpdate(), date_s_iso8601(), date_s_jd(), date_s_jisx0301(), date_s_ordinal(), date_s_parse(), date_s_rfc2822(), date_s_rfc3339(), date_s_strptime(), date_s_today(), date_s_valid_civil_p(), date_s_valid_commercial_p(), date_s_valid_jd_p(), date_s_valid_ordinal_p(), date_s_xmlschema(), date_strftime_internal(), datetime_s_civil(), datetime_s_commercial(), datetime_s_httpdate(), datetime_s_iso8601(), datetime_s_jd(), datetime_s_jisx0301(), datetime_s_now(), datetime_s_ordinal(), datetime_s_parse(), datetime_s_rfc2822(), datetime_s_rfc3339(), datetime_s_strptime(), datetime_s_xmlschema(), define_final(), dir_initialize(), dir_s_chdir(), dir_s_glob(), dir_s_home(), dir_s_mkdir(), do_checksum(), dt_lite_iso8601(), dt_lite_jisx0301(), econv_args(), econv_primitive_convert(), econv_putback(), enc_dump(), enum_chunk(), enum_count(), enum_cycle(), enum_find(), enum_find_index(), enum_first(), enum_inject(), enum_slice_before(), enumerator_with_index(), env_fetch(), etc_getgrgid(), etc_getpwuid(), ev_advise(), ev_on_event(), exc_initialize(), f_round_common(), fdbm_fetch_m(), fdbm_initialize(), fev_off_event(), fgdbm_fetch_m(), fgdbm_initialize(), file_s_fnmatch(), fix_to_s(), flo_round(), float_rationalize(), fole_initialize(), fole_s_connect(), fole_s_const_load(), fole_s_show_help(), fsdbm_fetch_m(), fsdbm_initialize(), gc_profile_report(), gc_stat(), generator_initialize(), gzreader_gets(), iconv_iconv(), iconv_initialize(), iconv_s_open(), initialize(), int_round(), integer_rationalize(), interrupt_init(), io_getpartial(), io_read(), io_s_write(), io_wait(), ip_cancel_eval(), ip_cancel_eval_unwind(), ip_create_slave(), ip_fromUTF8(), ip_init(), ip_toUTF8(), iseq_s_compile(), iseq_s_compile_file(), iseq_s_load(), lib_do_one_event_core(), lib_fromUTF8(), lib_mainloop(), lib_mainloop_watchdog(), lib_thread_callback(), lib_toUTF8(), marshal_dump(), marshal_load(), match_aref(), math_log(), memsize_of_all_m(), mSyslog_open(), mutex_sleep(), nilclass_rationalize(), nucomp_rationalize(), nucomp_s_convert(), nucomp_s_new(), nucomp_s_polar(), nurat_rationalize(), nurat_s_convert(), nurat_s_new(), obj_respond_to(), ole_create_dcom(), ole_invoke(), os_each_obj(), ossl_asn1_initialize(), ossl_bn_initialize(), ossl_bn_is_prime(), ossl_bn_is_prime_fasttest(), ossl_bn_s_generate_prime(), ossl_bn_to_s(), ossl_cipher_init(), ossl_cipher_pkcs5_keyivgen(), ossl_cipher_update(), ossl_dh_initialize(), ossl_dh_s_generate(), ossl_digest_finish(), ossl_digest_initialize(), ossl_dsa_export(), ossl_dsa_initialize(), ossl_pkcs12_initialize(), ossl_pkcs12_s_create(), ossl_pkcs7_decrypt(), ossl_pkcs7_initialize(), ossl_pkcs7_s_encrypt(), ossl_pkcs7_s_sign(), ossl_pkcs7_s_write_smime(), ossl_pkcs7_verify(), ossl_pkey_new_from_data(), ossl_rsa_export(), ossl_rsa_initialize(), ossl_rsa_private_decrypt(), ossl_rsa_private_encrypt(), ossl_rsa_public_decrypt(), ossl_rsa_public_encrypt(), ossl_rsa_s_generate(), ossl_spki_initialize(), ossl_ssl_initialize(), ossl_ssl_read_internal(), ossl_sslctx_flush_sessions(), ossl_sslctx_initialize(), ossl_x509_initialize(), ossl_x509attr_initialize(), ossl_x509crl_initialize(), ossl_x509ext_initialize(), ossl_x509extfactory_create_ext(), ossl_x509extfactory_initialize(), ossl_x509name_add_entry(), ossl_x509name_initialize(), ossl_x509name_to_s(), ossl_x509req_initialize(), ossl_x509stctx_initialize(), ossl_x509store_verify(), parse(), path_basename(), path_binread(), path_each_line(), path_expand_path(), path_fnmatch(), path_mkdir(), path_open(), path_read(), path_readlines(), path_realdirpath(), path_realpath(), path_s_glob(), path_sysopen(), prepare_getline_args(), proc_curry(), proc_wait(), pty_check(), random_init(), range_first(), range_initialize(), range_step(), rawmode_opt(), rb_ary_aref(), rb_ary_count(), rb_ary_cycle(), rb_ary_fetch(), rb_ary_fill(), rb_ary_flatten(), rb_ary_flatten_bang(), rb_ary_index(), rb_ary_initialize(), rb_ary_join_m(), rb_ary_permutation(), rb_ary_rindex(), rb_ary_rotate_bang(), rb_ary_rotate_m(), rb_ary_sample(), rb_ary_slice_bang(), rb_big_to_s(), rb_class_initialize(), rb_deflate_deflate(), rb_deflate_flush(), rb_deflate_initialize(), rb_deflate_s_deflate(), rb_digest_instance_digest(), rb_digest_instance_hexdigest(), rb_dlcfunc_initialize(), rb_dlhandle_initialize(), rb_dlptr_aref(), rb_dlptr_aset(), rb_dlptr_initialize(), rb_dlptr_s_malloc(), rb_dlptr_to_s(), rb_dlptr_to_str(), rb_f_abort(), rb_f_caller(), rb_f_catch(), rb_f_eval(), rb_f_exit(), rb_f_exit_bang(), rb_f_integer(), rb_f_load(), rb_f_rand(), rb_f_select(), rb_f_srand(), rb_f_throw(), rb_f_trace_var(), rb_f_untrace_var(), rb_file_s_absolute_path(), rb_file_s_basename(), rb_file_s_chmod(), rb_file_s_chown(), rb_file_s_expand_path(), rb_file_s_realdirpath(), rb_file_s_realpath(), rb_file_s_utime(), rb_gzreader_initialize(), rb_gzreader_read(), rb_gzreader_readpartial(), rb_gzwriter_flush(), rb_gzwriter_initialize(), rb_hash_default(), rb_hash_fetch_m(), rb_hash_initialize(), rb_inflate_initialize(), rb_io_advise(), rb_io_initialize(), rb_io_ioctl(), rb_io_reopen(), rb_io_s_binread(), rb_io_s_copy_stream(), rb_io_s_foreach(), rb_io_s_pipe(), rb_io_s_popen(), rb_io_s_read(), rb_io_s_readlines(), rb_io_s_sysopen(), rb_io_seek_m(), rb_io_set_encoding(), rb_io_sysread(), rb_io_sysseek(), rb_mod_const_defined(), rb_mod_const_get(), rb_mod_constants(), rb_obj_display(), rb_obj_methods(), rb_obj_singleton_methods(), rb_reg_match_m(), rb_reg_s_last_match(), rb_scan_open_args(), rb_str_chomp_bang(), rb_str_each_line(), rb_str_index_m(), rb_str_init(), rb_str_justify(), rb_str_rindex_m(), rb_str_split_m(), rb_str_sum(), rb_str_to_i(), rb_str_upto(), rb_struct_s_def(), readline_readline(), rsock_bsock_send(), rsock_s_recvfrom(), rsock_s_recvfrom_nonblock(), sock_initialize(), sock_s_getaddrinfo(), sock_s_gethostbyaddr(), sock_s_getnameinfo(), sock_s_getservbyname(), sock_s_getservbyport(), str_transcode(), strio_getline(), strio_init(), strio_seek(), strio_set_encoding(), strscan_initialize(), syck_emitter_emit(), syck_emitter_reset(), syck_out_map(), syck_out_scalar(), syck_out_seq(), syck_parser_initialize(), syck_parser_load(), syck_parser_load_documents(), syserr_initialize(), tcp_init(), tcp_svr_init(), thread_join_m(), time_arg(), time_dump(), time_getlocaltime(), time_init_1(), time_localtime_m(), time_round(), time_s_at(), tk_do_callback(), tk_eval_cmd(), tk_get_eval_string(), tk_install_cmd(), udp_init(), udp_send(), and window_box().

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

Definition at line 1224 of file class.c.

References NOEX_UNDEF, rb_add_method(), rb_intern, and VM_METHOD_TYPE_UNDEF.

Referenced by Init_Binding(), Init_Complex(), Init_curses(), Init_date_core(), Init_Encoding(), Init_eval(), Init_Numeric(), Init_Object(), Init_ossl_asn1(), Init_ossl_ssl_session(), Init_pathname(), Init_Proc(), Init_process(), Init_Rational(), Init_Regexp(), Init_String(), Init_VM(), rsock_init_basicsocket(), rsock_init_ipsocket(), and ruby_Init_Continuation_body().