Implement String#chr

This commit is contained in:
Jun Hiroe
2014-06-03 19:48:41 +09:00
parent 9fe8653e86
commit e67817fcec
2 changed files with 20 additions and 0 deletions
+16
View File
@@ -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
+4
View File
@@ -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