inspect()
public
Returns the representation of sym as a symbol literal.
:fred.inspect
Show source
static VALUE
sym_inspect(VALUE sym)
{
VALUE str;
ID id = SYM2ID(sym);
rb_encoding *enc;
const char *ptr;
long len;
char *dest;
rb_encoding *resenc = rb_default_internal_encoding();
if (resenc == NULL) resenc = rb_default_external_encoding();
sym = rb_id2str(id);
enc = STR_ENC_GET(sym);
ptr = RSTRING_PTR(sym);
len = RSTRING_LEN(sym);
if ((resenc != enc && !rb_str_is_ascii_only_p(sym)) || len != (long)strlen(ptr) ||
!rb_enc_symname_p(ptr, enc) || !sym_printable(ptr, ptr + len, enc)) {
str = rb_str_inspect(sym);
len = RSTRING_LEN(str);
rb_str_resize(str, len + 1);
dest = RSTRING_PTR(str);
memmove(dest + 1, dest, len);
dest[0] = ':';
}
else {
char *dest;
str = rb_enc_str_new(0, len + 1, enc);
dest = RSTRING_PTR(str);
dest[0] = ':';
memcpy(dest + 1, ptr, len);
}
return str;
}