Skip to content

Commit

Permalink
add Integer#chr method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kouki Ooyatsu committed Mar 6, 2013
1 parent 0f33126 commit cb3813c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/numeric.c
Expand Up @@ -262,6 +262,21 @@ flodivmod(mrb_state *mrb, mrb_float x, mrb_float y, mrb_float *divp, mrb_float *
if (modp) *modp = mod;
if (divp) *divp = div;
}

static mrb_value
int_chr(mrb_state *mrb, mrb_value x)
{
mrb_int chr;
char c;

chr = mrb_fixnum(x);
if (chr >= (1 << CHAR_BIT)) {
mrb_raisef(mrb, E_RANGE_ERROR, "%ld out of char range", chr);
}
c = (char)chr;

return mrb_str_new(mrb, &c, 1);
}

/* 15.2.9.3.5 */
/*
Expand Down Expand Up @@ -1338,6 +1353,7 @@ mrb_init_numeric(mrb_state *mrb)
mrb_define_method(mrb, fl, "to_f", flo_to_f, ARGS_NONE()); /* 15.2.9.3.13 */
mrb_define_method(mrb, fl, "to_i", flo_truncate, ARGS_NONE()); /* 15.2.9.3.14 */
mrb_define_method(mrb, fl, "truncate", flo_truncate, ARGS_NONE()); /* 15.2.9.3.15 */
mrb_define_method(mrb, fixnum, "chr", int_chr, ARGS_NONE());

mrb_define_method(mrb, fl, "to_s", flo_to_s, ARGS_NONE()); /* 15.2.9.3.16(x) */
mrb_define_method(mrb, fl, "inspect", flo_to_s, ARGS_NONE());
Expand Down

0 comments on commit cb3813c

Please sign in to comment.