Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
object.c
Go to the documentation of this file.
1 #include "mruby.h"
2 #include "mruby/array.h"
3 #include "mruby/class.h"
4 
5 /*
6  * call-seq:
7  * nil.to_a -> []
8  *
9  * Always returns an empty array.
10  */
11 
12 static mrb_value
13 nil_to_a(mrb_state *mrb, mrb_value obj)
14 {
15  return mrb_ary_new(mrb);
16 }
17 
18 /*
19  * call-seq:
20  * nil.to_f -> 0.0
21  *
22  * Always returns zero.
23  */
24 
25 static mrb_value
26 nil_to_f(mrb_state *mrb, mrb_value obj)
27 {
28  return mrb_float_value(mrb, 0.0);
29 }
30 
31 /*
32  * call-seq:
33  * nil.to_i -> 0
34  *
35  * Always returns zero.
36  */
37 
38 static mrb_value
39 nil_to_i(mrb_state *mrb, mrb_value obj)
40 {
41  return mrb_fixnum_value(0);
42 }
43 
44 /*
45  * call-seq:
46  * obj.instance_exec(arg...) {|var...| block } -> obj
47  *
48  * Executes the given block within the context of the receiver
49  * (_obj_). In order to set the context, the variable +self+ is set
50  * to _obj_ while the code is executing, giving the code access to
51  * _obj_'s instance variables. Arguments are passed as block parameters.
52  *
53  * class KlassWithSecret
54  * def initialize
55  * @secret = 99
56  * end
57  * end
58  * k = KlassWithSecret.new
59  * k.instance_exec(5) {|x| @secret+x } #=> 104
60  */
61 
63 mrb_yield_internal(mrb_state *mrb, mrb_value b, int argc, mrb_value *argv, mrb_value self, struct RClass *c);
64 
65 static mrb_value
66 mrb_obj_instance_exec(mrb_state *mrb, mrb_value self)
67 {
68  mrb_value *argv;
69  int argc;
70  mrb_value blk;
71  struct RClass *c;
72 
73  mrb_get_args(mrb, "*&", &argv, &argc, &blk);
74 
75  if (mrb_nil_p(blk)) {
76  mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
77  }
78 
79  switch (mrb_type(self)) {
80  case MRB_TT_SYMBOL:
81  case MRB_TT_FIXNUM:
82  case MRB_TT_FLOAT:
83  c = NULL;
84  break;
85  default:
86  c = mrb_class_ptr(mrb_singleton_class(mrb, self));
87  break;
88  }
89 
90  return mrb_yield_internal(mrb, blk, argc, argv, self, c);
91 }
92 
93 void
95 {
96  struct RClass * n = mrb->nil_class;
97 
98  mrb_define_method(mrb, n, "to_a", nil_to_a, MRB_ARGS_NONE());
99  mrb_define_method(mrb, n, "to_f", nil_to_f, MRB_ARGS_NONE());
100  mrb_define_method(mrb, n, "to_i", nil_to_i, MRB_ARGS_NONE());
101 
102  mrb_define_method(mrb, mrb->object_class, "instance_exec", mrb_obj_instance_exec, MRB_ARGS_ANY() | MRB_ARGS_BLOCK());
103 }
104 
105 void
107 {
108 }