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;
unsigned int i = NUM2UINT(num);
rb_encoding *enc;
switch (argc) {
case 0:
if (i < 0) {
out_of_range:
rb_raise(rb_eRangeError, "%d out of char range", i);
}
if (0xff < i) {
enc = rb_default_internal_encoding();
if (!enc) goto out_of_range;
goto decode;
}
c = (char)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..1)", argc);
break;
}
enc = rb_to_encoding(argv[0]);
if (!enc) enc = rb_ascii8bit_encoding();
decode:
return rb_enc_uint_chr(i, enc);
}