Commit 2ff67f8d authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #2618 from suzukaze/add-kernel.Float

Add Kernel#Float
parents d3c7e117 140e9bbf
...@@ -23,6 +23,27 @@ mrb_f_method(mrb_state *mrb, mrb_value self) ...@@ -23,6 +23,27 @@ mrb_f_method(mrb_state *mrb, mrb_value self)
return mrb_nil_value(); 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: * call-seq:
* String(arg) -> string * String(arg) -> string
...@@ -116,6 +137,7 @@ mrb_mruby_kernel_ext_gem_init(mrb_state *mrb) ...@@ -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_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_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, "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, "Array", mrb_f_array, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, krn, "Hash", mrb_f_hash, 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 ...@@ -17,6 +17,13 @@ assert('Kernel#__method__') do
assert_equal(:m2, c.new.m2) assert_equal(:m2, c.new.m2)
end 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('Kernel#String') do
assert_equal("main", String(self)) assert_equal("main", String(self))
assert_equal("Object", String(self.class)) 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