instance method
Module#class_exec
[U] (*untyped, **untyped) { (?) [self: self] -> U } -> U
mod.class_exec(arg...) {|var...| block } → obj
vm_eval.c
View on GitHub ↗
static VALUE
rb_mod_module_exec_internal(int argc, const VALUE *argv, VALUE mod)
{
return yield_under(mod, FALSE, argc, argv, RB_PASS_CALLED_KEYWORDS);
}
Alias of #module_exec
Evaluates the given block in the context of the class/module. The method defined in the block will belong to the receiver. Any arguments passed to the method will be passed to the block. This can be used if the block needs to access instance variables.
Example
class Thing end Thing.class_exec{ def hello() "Hello there!" end } puts Thing.new.hello()
produces:
Example
Hello there!
See also
#module_exec
#class_eval
#class_variable_defined?
#autoload?
also in
4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0