Commit 140e9bbf authored by Jun Hiroe's avatar Jun Hiroe

Add Kernel.Float

parent d3c7e117
......@@ -23,6 +23,27 @@ mrb_f_method(mrb_state *mrb, mrb_value self)
return mrb_nil_value();
}
/*
* call-seq:
* Float(arg) -> float
*
* Returns <i>arg</i> converted to a float. Numeric types are converted
* directly, the rest are converted using <i>arg</i>.to_f.
*
* Float(1) #=> 1.0
* Float(123.456) #=> 123.456
* Float("123.456") #=> 123.456
* Float(nil) #=> TypeError
*/
static mrb_value
mrb_f_float(mrb_state *mrb, mrb_value self)
{
mrb_value arg;
mrb_get_args(mrb, "o", &arg);
return mrb_Float(mrb, arg);
}
/*
* call-seq:
* String(arg) -> string
......@@ -116,6 +137,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, "Float", mrb_f_float, MRB_ARGS_REQ(1));
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));
mrb_define_module_function(mrb, krn, "Hash", mrb_f_hash, MRB_ARGS_REQ(1));
......
......@@ -17,6 +17,13 @@ assert('Kernel#__method__') do
assert_equal(:m2, c.new.m2)
end
assert('Kernel#Float') do
assert_equal(1.0, Float(1))
assert_equal(123.456, Float(123.456))
assert_equal(123.456, Float("123.456"))
assert_raise(TypeError) { Float(nil) }
end
assert('Kernel#String') do
assert_equal("main", String(self))
assert_equal("Object", String(self.class))
......
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