method
inspect
ruby latest stable - Class:
String
inspect()public
Returns a printable version of str, surrounded by quote marks, with special characters escaped.
str = "hello" str[3] = "\b" str.inspect #=> "\"hel\\bo\""
VALUE
rb_str_inspect(VALUE str)
{
int encidx = ENCODING_GET(str);
rb_encoding *enc = rb_enc_from_index(encidx), *actenc;
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;
actenc = get_actual_encoding(encidx, str);
if (actenc != enc) {
enc = actenc;
if (unicode_p) unicode_p = rb_enc_unicode_p(enc);
}
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;
}
if ((enc == resenc && rb_enc_isprint(c, enc)) ||
(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, "\"");
OBJ_INFECT_RAW(result, str);
return result;
} Related methods
- Instance methods
- %
- *
- +
- +@
- -@
- <<
- <=>
- ==
- ===
- =~
- []
- []=
- ascii_only?
- b
- block_scanf
- bytes
- bytesize
- byteslice
- capitalize
- capitalize!
- casecmp
- casecmp?
- center
- chars
- chomp
- chomp!
- chop
- chop!
- chr
- clear
- codepoints
- concat
- count
- crypt
- delete
- delete!
- delete_prefix
- delete_prefix!
- delete_suffix
- delete_suffix!
- downcase
- downcase!
- dump
- each_byte
- each_char
- each_codepoint
- each_grapheme_cluster
- each_line
- empty?
- encode
- encode!
- encoding
- end_with?
- eql?
- force_encoding
- freeze
- getbyte
- grapheme_clusters
- gsub
- gsub!
- hash
- hex
- include?
- index
- initialize_copy
- insert
- inspect
- intern
- iseuc
- isjis
- issjis
- isutf8
- kconv
- length
- lines
- ljust
- lstrip
- lstrip!
- match
- match?
- next
- next!
- oct
- ord
- parse_csv
- partition
- prepend
- pretty_print
- replace
- reverse
- reverse!
- rindex
- rjust
- rpartition
- rstrip
- rstrip!
- scan
- scanf
- scrub
- scrub!
- setbyte
- shellescape
- shellsplit
- size
- slice
- slice!
- split
- squeeze
- squeeze!
- start_with?
- strip
- strip!
- sub
- sub!
- succ
- succ!
- sum
- swapcase
- swapcase!
- to_c
- to_d
- to_f
- to_i
- to_r
- to_s
- to_str
- to_sym
- toeuc
- tojis
- tolocale
- tosjis
- toutf16
- toutf32
- toutf8
- tr
- tr!
- tr_s
- tr_s!
- undump
- unicode_normalize
- unicode_normalize!
- unicode_normalized?
- unpack
- unpack1
- upcase
- upcase!
- upto
- valid_encoding?
- Class methods
- new
- try_convert