Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

add Integer#chr method #955

Closed
wants to merge 1 commit into from

1 participant

@ghost

for String#each_byte

@matz
Owner

I vaguely think those extended (non ISO) methods should be provided by mrbgem.
What do you think?

@ghost

Thanks for the comment.
Discussion in #959 until decided, I'll not close this request.

I agree to @matz.
However, I don't want that the method will be added randomly to mruby(or mrbgem)

In either case, write to the #959 in my opinion.

@matz
Owner

As String#getbyte (#956), String#chr should go to mruby-string-ext (#963)

@matz matz closed this
@ghost ghost deleted the iij:pr-integer-chr branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 6, 2013
  1. add Integer#chr method

    Kouki Ooyatsu authored
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 0 deletions.
  1. +16 −0 src/numeric.c
View
16 src/numeric.c
@@ -263,6 +263,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 */
/*
@@ -1374,6 +1389,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());
Something went wrong with that request. Please try again.