Commit e67817fc authored by Jun Hiroe's avatar Jun Hiroe

Implement String#chr

parent 9fe8653e
......@@ -175,6 +175,21 @@ mrb_str_oct(mrb_state *mrb, mrb_value self)
return mrb_str_to_inum(mrb, self, 8, FALSE);
}
/*
* call-seq:
* string.chr -> string
*
* Returns a one-character string at the beginning of the string.
*
* a = "abcde"
* a.chr #=> "a"
*/
static mrb_value
mrb_str_chr(mrb_state *mrb, mrb_value self)
{
return mrb_str_substr(mrb, self, 0, 1);
}
void
mrb_mruby_string_ext_gem_init(mrb_state* mrb)
{
......@@ -190,6 +205,7 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
mrb_define_method(mrb, s, "end_with?", mrb_str_end_with, MRB_ARGS_REST());
mrb_define_method(mrb, s, "hex", mrb_str_hex, MRB_ARGS_NONE());
mrb_define_method(mrb, s, "oct", mrb_str_oct, MRB_ARGS_NONE());
mrb_define_method(mrb, s, "chr", mrb_str_chr, MRB_ARGS_NONE());
}
void
......
......@@ -161,3 +161,7 @@ assert('String#oct') do
assert_equal 8, "010".oct
assert_equal (-8), "-10".oct
end
assert('String#chr') do
assert_equal "a", "abcde".chr
end
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