module_function(*args)
private
Creates module functions for the named methods. These functions may be
called with the module as a receiver, and also become available as instance
methods to classes that mix in the module. Module functions are copies of the original, and so
may be changed independently. The instance-method versions are made private. If used with no arguments,
subsequently defined methods become module functions.
module Mod
def one
"This is one"
end
module_function :one
end
class Cls
include Mod
def call_one
one
end
end
Mod.one
c = Cls.new
c.call_one
module Mod
def one
"This is the new one"
end
end
Mod.one
c.call_one
Show source
static VALUE
rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
{
int i;
ID id;
const rb_method_entry_t *me;
if (TYPE(module) != T_MODULE) {
rb_raise(rb_eTypeError, "module_function must be called for modules");
}
secure_visibility(module);
if (argc == 0) {
SCOPE_SET(NOEX_MODFUNC);
return module;
}
set_method_visibility(module, argc, argv, NOEX_PRIVATE);
for (i = 0; i < argc; i++) {
VALUE m = module;
id = rb_to_id(argv[i]);
for (;;) {
me = search_method(m, id);
if (me == 0) {
me = search_method(rb_cObject, id);
}
if (UNDEFINED_METHOD_ENTRY_P(me)) {
rb_print_undef(module, id, 0);
}
if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
break; /* normal case: need not to follow 'super' link */
}
m = RCLASS_SUPER(m);
if (!m)
break;
}
rb_method_entry_set(rb_singleton_class(module), id, me, NOEX_PUBLIC);
}
return module;
}