method
inspect

Related methods
- Class methods (3)
-
new
-
try_convert
-
yaml_new (<= v1_9_1_378)
- Instance methods (168)
-
<<
-
<=>
-
=~
-
==
-
===
-
-@ (>= v2_4_6)
-
[]
-
[]=
-
*
-
%
-
+
-
+@ (>= v2_4_6)
-
ascii_only?
-
b (>= v2_1_10)
-
block_scanf
-
bytes
-
bytesize
-
byteslice
-
capitalize
-
capitalize!
-
casecmp
-
casecmp? (>= v2_4_6)
-
center
-
chars
-
chomp
-
chomp!
-
chop
-
chop!
-
chr
-
clear
-
codepoints
-
concat
-
count
-
crypt
-
delete
-
delete!
-
delete_prefix (>= v2_5_5)
-
delete_prefix! (>= v2_5_5)
-
delete_suffix (>= v2_5_5)
-
delete_suffix! (>= v2_5_5)
-
downcase
-
downcase!
-
dump
-
each (<= v1_8_7_330)
-
each_byte
-
each_char
-
each_codepoint
-
each_grapheme_cluster (>= v2_5_5)
-
each_line
-
empty?
-
encode
-
encode!
-
encoding
-
end_regexp (<= v1_8_7_330)
-
end_with?
-
eql?
-
_expand_ch
(<= v1_8_7_330)
-
expand_ch_hash
(<= v1_8_7_330)
-
ext
-
force_encoding
-
freeze (>= v2_4_6)
-
getbyte
-
grapheme_clusters (>= v2_5_5)
-
gsub
-
gsub!
-
hash
-
hex
-
include?
-
index
-
initialize_copy
-
insert
-
inspect
-
intern
-
is_binary_data? (<= v1_9_1_378)
-
is_complex_yaml? (<= v1_9_1_378)
-
iseuc
-
isjis
-
issjis
-
isutf8
-
jcount (<= v1_8_7_330)
-
jlength (<= v1_8_7_330)
-
jsize (<= v1_8_7_330)
-
kconv
-
length
-
lines
-
ljust
-
lstrip
-
lstrip!
-
match
-
match? (>= v2_4_6)
-
mbchar? (<= v1_8_7_330)
-
next
-
next!
-
oct
-
ord
-
original_succ
(<= v1_8_7_330)
-
original_succ!
(<= v1_8_7_330)
-
parse_csv
-
partition
-
pathmap
-
pathmap_explode
-
pathmap_partial
-
pathmap_replace
-
prepend
-
pretty_print (>= v2_4_6)
-
quote (<= v1_8_6_287)
-
_regex_quote
(<= v1_8_7_330)
-
replace
-
reverse
-
reverse!
-
rindex
-
rjust
-
rpartition
-
rstrip
-
rstrip!
-
scan
-
scanf
-
scrub (>= v2_1_10)
-
scrub! (>= v2_1_10)
-
setbyte
-
shellescape
-
shellsplit
-
size
-
slice
-
slice!
-
split
-
squeeze
-
squeeze!
-
start_with?
-
strip
-
strip!
-
sub
-
sub!
-
succ
-
succ!
-
sum
-
swapcase
-
swapcase!
-
to_c
-
to_d
-
toeuc
-
to_f
-
to_i
-
tojis
-
tolocale
-
to_r
-
to_s
-
tosjis
-
to_str
-
to_sym
-
toutf16
-
toutf32
-
toutf8
-
to_yaml (<= v1_9_1_378)
-
tr
-
tr!
-
tr_s
-
tr_s!
-
undump (>= v2_5_5)
-
unicode_normalize (>= v2_2_9)
-
unicode_normalize! (>= v2_2_9)
-
unicode_normalized? (>= v2_2_9)
-
unpack
-
unpack1 (>= v2_4_6)
-
upcase
-
upcase!
-
upto
-
valid_encoding?
= private
= protected
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\""
Show source
VALUE rb_str_inspect(VALUE str) { rb_encoding *enc = STR_ENC_GET(str); 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); static rb_encoding *utf16, *utf32; if (!utf16) utf16 = rb_enc_find("UTF-16"); if (!utf32) utf32 = rb_enc_find("UTF-32"); 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; if (enc == utf16) { const unsigned char *q = (const unsigned char *)p; if (q[0] == 0xFE && q[1] == 0xFF) enc = rb_enc_find("UTF-16BE"); else if (q[0] == 0xFF && q[1] == 0xFE) enc = rb_enc_find("UTF-16LE"); else unicode_p = 0; } else if (enc == utf32) { const unsigned char *q = (const unsigned char *)p; if (q[0] == 0 && q[1] == 0 && q[2] == 0xFE && q[3] == 0xFF) enc = rb_enc_find("UTF-32BE"); else if (q[3] == 0 && q[2] == 0 && q[1] == 0xFE && q[0] == 0xFF) enc = rb_enc_find("UTF-32LE"); else unicode_p = 0; } 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(result, str); return result; }