Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Symbol#inspect
() -> ::String
inspect → string
string.c
View on GitHub ↗
static VALUE
sym_inspect(VALUE sym)
{
VALUE str = rb_sym2str(sym);
const char *ptr;
long len;
char *dest;
if (!rb_str_symname_p(str)) {
str = rb_str_inspect(str);
len = RSTRING_LEN(str);
rb_str_resize(str, len + 1);
dest = RSTRING_PTR(str);
memmove(dest + 1, dest, len);
}
else {
rb_encoding *enc = STR_ENC_GET(str);
VALUE orig_str = str;
len = RSTRING_LEN(orig_str);
str = rb_enc_str_new(0, len + 1, enc);
// Get data pointer after allocation
ptr = RSTRING_PTR(orig_str);
dest = RSTRING_PTR(str);
memcpy(dest + 1, ptr, len);
RB_GC_GUARD(orig_str);
}
dest[0] = ':';
RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING);
return str;
}
Returns a string representation of self (including the leading colon):
Example
:foo.inspect # => ":foo"
Related: Symbol#to_s, Symbol#name.