chr(*args)
public
Returns a string containing the character represented by the receiver’s
value according to encoding.
65.chr
230.chr
255.chr(Encoding::UTF_8)
Show source
static VALUE
int_chr(int argc, VALUE *argv, VALUE num)
{
char c;
int n;
long i = NUM2LONG(num);
rb_encoding *enc;
VALUE str;
switch (argc) {
case 0:
if (i < 0 || 0xff < i) {
out_of_range:
rb_raise(rb_eRangeError, "%"PRIdVALUE " out of char range", i);
}
c = i;
if (i < 0x80) {
return rb_usascii_str_new(&c, 1);
}
else {
return rb_str_new(&c, 1);
}
case 1:
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
break;
}
enc = rb_to_encoding(argv[0]);
if (!enc) enc = rb_ascii8bit_encoding();
if (i < 0 || (n = rb_enc_codelen(i, enc)) <= 0) goto out_of_range;
str = rb_enc_str_new(0, n, enc);
rb_enc_mbcput(i, RSTRING_PTR(str), enc);
return str;
}