Commit de04d3f0 authored by Jun Hiroe's avatar Jun Hiroe

Add Kernel.String

parent 69ba4f0e
......@@ -22,6 +22,31 @@ mrb_f_method(mrb_state *mrb, mrb_value self)
return mrb_nil_value();
}
/*
* call-seq:
* String(arg) -> string
*
* Returns <i>arg</i> as an <code>String</code>.
*
* First tries to call its <code>to_str</code> method, then its to_s method.
*
* String(self) #=> "main"
* String(self.class) #=> "Object"
* String(123456) #=> "123456"
*/
static mrb_value
mrb_f_string(mrb_state *mrb, mrb_value self)
{
mrb_value arg, tmp;
mrb_get_args(mrb, "o", &arg);
tmp = mrb_check_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_str");
if (mrb_nil_p(tmp)) {
tmp = mrb_check_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_s");
}
return tmp;
}
/*
* call-seq:
* Array(arg) -> array
......@@ -57,6 +82,7 @@ mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
mrb_define_module_function(mrb, krn, "fail", mrb_f_raise, MRB_ARGS_OPT(2));
mrb_define_method(mrb, krn, "__method__", mrb_f_method, MRB_ARGS_NONE());
mrb_define_module_function(mrb, krn, "String", mrb_f_string, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, krn, "Array", mrb_f_array, MRB_ARGS_REQ(1));
}
......
......@@ -17,6 +17,12 @@ assert('Kernel#__method__') do
assert_equal(:m2, c.new.m2)
end
assert('Kernel#String') do
assert_equal("main", String(self))
assert_equal("Object", String(self.class))
assert_equal("123456", String(123456))
end
assert('Kernel#Array') do
assert_equal([1], Kernel.Array(1))
assert_equal([1, 2, 3, 4, 5], Kernel.Array([1, 2, 3, 4, 5]))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment