mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Implement String#chr
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user