2 #include <mruby/array.h>
3 #include <mruby/class.h>
4 #include <mruby/proc.h>
10 * Always returns an empty array.
14 nil_to_a(mrb_state *mrb, mrb_value obj)
16 return mrb_ary_new(mrb);
19 #ifndef MRB_WITHOUT_FLOAT
24 * Always returns zero.
28 nil_to_f(mrb_state *mrb, mrb_value obj)
30 return mrb_float_value(mrb, 0.0);
38 * Always returns zero.
42 nil_to_i(mrb_state *mrb, mrb_value obj)
44 return mrb_fixnum_value(0);
49 * obj.instance_exec(arg...) {|var...| block } -> obj
51 * Executes the given block within the context of the receiver
52 * (_obj_). In order to set the context, the variable +self+ is set
53 * to _obj_ while the code is executing, giving the code access to
54 * _obj_'s instance variables. Arguments are passed as block parameters.
56 * class KlassWithSecret
61 * k = KlassWithSecret.new
62 * k.instance_exec(5) {|x| @secret+x } #=> 104
66 mrb_obj_instance_exec(mrb_state *mrb, mrb_value self)
68 const mrb_value *argv;
73 mrb_get_args(mrb, "*&", &argv, &argc, &blk);
76 mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
79 switch (mrb_type(self)) {
82 #ifndef MRB_WITHOUT_FLOAT
88 c = mrb_class_ptr(mrb_singleton_class(mrb, self));
91 mrb->c->ci->target_class = c;
92 return mrb_yield_cont(mrb, blk, self, argc, argv);
96 mrb_mruby_object_ext_gem_init(mrb_state* mrb)
98 struct RClass * n = mrb->nil_class;
100 mrb_define_method(mrb, n, "to_a", nil_to_a, MRB_ARGS_NONE());
101 #ifndef MRB_WITHOUT_FLOAT
102 mrb_define_method(mrb, n, "to_f", nil_to_f, MRB_ARGS_NONE());
104 mrb_define_method(mrb, n, "to_i", nil_to_i, MRB_ARGS_NONE());
106 mrb_define_method(mrb, mrb->kernel_module, "instance_exec", mrb_obj_instance_exec, MRB_ARGS_ANY() | MRB_ARGS_BLOCK());
110 mrb_mruby_object_ext_gem_final(mrb_state* mrb)