instance method
String#inspect
() -> ::String
inspect → string
string.c
View on GitHub ↗
VALUE
rb_str_inspect(VALUE str)
{
int encidx = ENCODING_GET(str);
rb_encoding *enc = rb_enc_from_index(encidx);
const char *p, *pend, *prev;
char buf[CHAR_ESC_LEN + 1];
VALUE result = rb_str_buf_new(0);
rb_encoding *resenc = rb_default_internal_encoding();
int unicode_p = rb_enc_unicode_p(enc);
int asciicompat = rb_enc_asciicompat(enc);
if (resenc == NULL) resenc = rb_default_external_encoding();
if (!rb_enc_asciicompat(resenc)) resenc = rb_usascii_encoding();
rb_enc_associate(result, resenc);
str_buf_cat2(result, "\"");
p = RSTRING_PTR(str); pend = RSTRING_END(str);
prev = p;
while (p < pend) {
unsigned int c, cc;
int n;
n = rb_enc_precise_mbclen(p, pend, enc);
if (!MBCLEN_CHARFOUND_P(n)) {
if (p > prev) str_buf_cat(result, prev, p - prev);
n = rb_enc_mbminlen(enc);
if (pend < p + n)
n = (int)(pend - p);
while (n--) {
snprintf(buf, CHAR_ESC_LEN, "\\x%02X", *p & 0377);
str_buf_cat(result, buf, strlen(buf));
prev = ++p;
}
continue;
}
n = MBCLEN_CHARFOUND_LEN(n);
c = rb_enc_mbc_to_codepoint(p, pend, enc);
p += n;
if ((asciicompat || unicode_p) &&
(c == '"'|| c == '\\' ||
(c == '#' &&
p < pend &&
MBCLEN_CHARFOUND_P(rb_enc_precise_mbclen(p,pend,enc)) &&
(cc = rb_enc_codepoint(p,pend,enc),
(cc == '$' || cc == '@' || cc == '{'))))) {
if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
str_buf_cat2(result, "\\");
if (asciicompat || enc == resenc) {
prev = p - n;
continue;
}
}
switch (c) {
case '\n': cc = 'n'; break;
case '\r': cc = 'r'; break;
case '\t': cc = 't'; break;
case '\f': cc = 'f'; break;
case '\013': cc = 'v'; break;
case '\010': cc = 'b'; break;
case '\007': cc = 'a'; break;
case 033: cc = 'e'; break;
default: cc = 0; break;
}
if (cc) {
if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
buf[0] = '\\';
buf[1] = (char)cc;
str_buf_cat(result, buf, 2);
prev = p;
continue;
}
/* The special casing of 0x85 (NEXT_LINE) here is because
* Oniguruma historically treats it as printable, but it
* doesn't match the print POSIX bracket class or character
* property in regexps.
*
* See Ruby Bug #16842 for details:
* https://bugs.ruby-lang.org/issues/16842
*/
if ((enc == resenc && rb_enc_isprint(c, enc) && c != 0x85) ||
(asciicompat && rb_enc_isascii(c, enc) && ISPRINT(c))) {
continue;
}
else {
if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
rb_str_buf_cat_escaped_char(result, c, unicode_p);
prev = p;
continue;
}
}
if (p > prev) str_buf_cat(result, prev, p - prev);
str_buf_cat2(result, "\"");
return result;
}
Returns a printable version of self, enclosed in double-quotes.
Most printable characters are rendered simply as themselves:
Example
'abc'.inspect # => "\"abc\"" '012'.inspect # => "\"012\"" ''.inspect # => "\"\"" "\u000012".inspect # => "\"\\u000012\"" 'тест'.inspect # => "\"тест\"" 'こんにちは'.inspect # => "\"こんにちは\""
But printable characters double-quote ('"') and backslash and ('\') are escaped:
Example
'"'.inspect # => "\"\\\"\"" '\\'.inspect # => "\"\\\\\""
Unprintable characters are the ASCII characters whose values are in range 0..31, along with the character whose value is 127.
Most of these characters are rendered thus:
Example
0.chr.inspect # => "\"\\x00\"" 1.chr.inspect # => "\"\\x01\"" 2.chr.inspect # => "\"\\x02\"" # ...
A few, however, have special renderings:
Example
7.chr.inspect # => "\"\\a\"" # BEL 8.chr.inspect # => "\"\\b\"" # BS 9.chr.inspect # => "\"\\t\"" # TAB 10.chr.inspect # => "\"\\n\"" # LF 11.chr.inspect # => "\"\\v\"" # VT 12.chr.inspect # => "\"\\f\"" # FF 13.chr.inspect # => "\"\\r\"" # CR 27.chr.inspect # => "\"\\e\"" # ESC
Related: see Converting to Non-String.