2 #include <mruby/error.h>
3 #include <mruby/array.h>
4 #include <mruby/hash.h>
5 #include <mruby/range.h>
8 mrb_f_caller(mrb_state *mrb, mrb_value self)
10 mrb_value bt, v, length;
11 mrb_int bt_len, argc, lev, n;
13 bt = mrb_get_backtrace(mrb);
14 bt_len = RARRAY_LEN(bt);
15 argc = mrb_get_args(mrb, "|oo", &v, &length);
23 if (mrb_type(v) == MRB_TT_RANGE) {
25 if (mrb_range_beg_len(mrb, v, &beg, &len, bt_len, TRUE) == 1) {
30 return mrb_nil_value();
34 v = mrb_to_int(mrb, v);
37 mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative level (%S)", v);
43 lev = mrb_fixnum(mrb_to_int(mrb, v));
44 n = mrb_fixnum(mrb_to_int(mrb, length));
46 mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative level (%S)", v);
49 mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative size (%S)", length);
58 return mrb_ary_new(mrb);
61 return mrb_funcall(mrb, bt, "[]", 2, mrb_fixnum_value(lev), mrb_fixnum_value(n));
66 * __method__ -> symbol
68 * Returns the name at the definition of the current method as a
70 * If called outside of a method, it returns <code>nil</code>.
74 mrb_f_method(mrb_state *mrb, mrb_value self)
76 mrb_callinfo *ci = mrb->c->ci;
79 return mrb_symbol_value(ci->mid);
81 return mrb_nil_value();
86 * Integer(arg,base=0) -> integer
88 * Converts <i>arg</i> to a <code>Fixnum</code>.
89 * Numeric types are converted directly (with floating point numbers
90 * being truncated). <i>base</i> (0, or between 2 and 36) is a base for
91 * integer string representation. If <i>arg</i> is a <code>String</code>,
92 * when <i>base</i> is omitted or equals to zero, radix indicators
93 * (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
94 * In any case, strings should be strictly conformed to numeric
95 * representation. This behavior is different from that of
96 * <code>String#to_i</code>. Non string values will be treated as integers.
97 * Passing <code>nil</code> raises a TypeError.
99 * Integer(123.999) #=> 123
100 * Integer("0x1a") #=> 26
101 * Integer(Time.new) #=> 1204973019
102 * Integer("0930", 10) #=> 930
103 * Integer("111", 2) #=> 7
104 * Integer(nil) #=> TypeError
107 mrb_f_integer(mrb_state *mrb, mrb_value self)
112 mrb_get_args(mrb, "o|i", &arg, &base);
113 return mrb_convert_to_integer(mrb, arg, base);
116 #ifndef MRB_WITHOUT_FLOAT
119 * Float(arg) -> float
121 * Returns <i>arg</i> converted to a float. Numeric types are converted
122 * directly, the rest are converted using <i>arg</i>.to_f.
125 * Float(123.456) #=> 123.456
126 * Float("123.456") #=> 123.456
127 * Float(nil) #=> TypeError
130 mrb_f_float(mrb_state *mrb, mrb_value self)
134 mrb_get_args(mrb, "o", &arg);
135 return mrb_Float(mrb, arg);
141 * String(arg) -> string
143 * Returns <i>arg</i> as an <code>String</code>.
144 * converted using <code>to_s</code> method.
146 * String(self) #=> "main"
147 * String(self.class) #=> "Object"
148 * String(123456) #=> "123456"
151 mrb_f_string(mrb_state *mrb, mrb_value self)
155 mrb_get_args(mrb, "o", &arg);
156 tmp = mrb_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_s");
162 * Array(arg) -> array
164 * Returns +arg+ as an Array using to_a method.
166 * Array(1..5) #=> [1, 2, 3, 4, 5]
170 mrb_f_array(mrb_state *mrb, mrb_value self)
174 mrb_get_args(mrb, "o", &arg);
175 tmp = mrb_check_convert_type(mrb, arg, MRB_TT_ARRAY, "Array", "to_a");
176 if (mrb_nil_p(tmp)) {
177 return mrb_ary_new_from_values(mrb, 1, &arg);
187 * Returns a <code>Hash</code> if <i>arg</i> is a <code>Hash</code>.
188 * Returns an empty <code>Hash</code> when <i>arg</i> is <tt>nil</tt>
193 * Hash(key: :value) #=> {:key => :value}
194 * Hash([1, 2, 3]) #=> TypeError
198 mrb_f_hash(mrb_state *mrb, mrb_value self)
202 mrb_get_args(mrb, "o", &arg);
203 if (mrb_nil_p(arg) || (mrb_array_p(arg) && RARRAY_LEN(arg) == 0)) {
204 return mrb_hash_new(mrb);
206 return mrb_ensure_hash_type(mrb, arg);
211 * obj.itself -> an_object
213 * Returns <i>obj</i>.
215 * string = 'my string' #=> "my string"
216 * string.itself.object_id == string.object_id #=> true
220 mrb_f_itself(mrb_state *mrb, mrb_value self)
226 mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
228 struct RClass *krn = mrb->kernel_module;
230 mrb_define_module_function(mrb, krn, "fail", mrb_f_raise, MRB_ARGS_OPT(2));
231 mrb_define_module_function(mrb, krn, "caller", mrb_f_caller, MRB_ARGS_OPT(2));
232 mrb_define_method(mrb, krn, "__method__", mrb_f_method, MRB_ARGS_NONE());
233 mrb_define_module_function(mrb, krn, "Integer", mrb_f_integer, MRB_ARGS_ANY());
234 #ifndef MRB_WITHOUT_FLOAT
235 mrb_define_module_function(mrb, krn, "Float", mrb_f_float, MRB_ARGS_REQ(1));
237 mrb_define_module_function(mrb, krn, "String", mrb_f_string, MRB_ARGS_REQ(1));
238 mrb_define_module_function(mrb, krn, "Array", mrb_f_array, MRB_ARGS_REQ(1));
239 mrb_define_module_function(mrb, krn, "Hash", mrb_f_hash, MRB_ARGS_REQ(1));
240 mrb_define_module_function(mrb, krn, "itself", mrb_f_itself, MRB_ARGS_NONE());
244 mrb_mruby_kernel_ext_gem_final(mrb_state *mrb)