2 #include "mruby/class.h"
3 #include "mruby/string.h"
6 mrb_mod_name(mrb_state *mrb, mrb_value self)
8 mrb_value name = mrb_class_path(mrb, mrb_class_ptr(self));
9 if (mrb_string_p(name)) {
10 MRB_SET_FROZEN_FLAG(mrb_basic_ptr(name));
16 mrb_mod_singleton_class_p(mrb_state *mrb, mrb_value self)
18 return mrb_bool_value(mrb_sclass_p(self));
23 * module_exec(arg...) {|var...| block } -> obj
24 * class_exec(arg...) {|var...| block } -> obj
26 * Evaluates the given block in the context of the
27 * class/module. The method defined in the block will belong
28 * to the receiver. Any arguments passed to the method will be
29 * passed to the block. This can be used if the block needs to
30 * access instance variables.
35 * def hello() "Hello there!" end
37 * puts Thing.new.hello()
41 mrb_mod_module_exec(mrb_state *mrb, mrb_value self)
43 const mrb_value *argv;
48 mrb_get_args(mrb, "*&!", &argv, &argc, &blk);
50 c = mrb_class_ptr(self);
51 if (mrb->c->ci->acc < 0) {
52 return mrb_yield_with_class(mrb, blk, argc, argv, self, c);
54 mrb->c->ci->target_class = c;
55 return mrb_yield_cont(mrb, blk, self, argc, argv);
59 mrb_mruby_class_ext_gem_init(mrb_state *mrb)
61 struct RClass *mod = mrb->module_class;
63 mrb_define_method(mrb, mod, "name", mrb_mod_name, MRB_ARGS_NONE());
64 mrb_define_method(mrb, mod, "singleton_class?", mrb_mod_singleton_class_p, MRB_ARGS_NONE());
65 mrb_define_method(mrb, mod, "module_exec", mrb_mod_module_exec, MRB_ARGS_ANY()|MRB_ARGS_BLOCK());
66 mrb_define_method(mrb, mod, "class_exec", mrb_mod_module_exec, MRB_ARGS_ANY()|MRB_ARGS_BLOCK());
70 mrb_mruby_class_ext_gem_final(mrb_state *mrb)