method
inspect
v1_8_7_330 -
Show latest stable
- Class:
String
inspect()public
Returns a printable version of str, with special characters escaped.
str = "hello" str[3] = 8 str.inspect #=> "hel\010o"
/*
* call-seq:
* str.inspect => string
*
* Returns a printable version of _str_, with special characters
* escaped.
*
* str = "hello"
* str[3] = 8
* str.inspect #=> "hel\010o"
*/
VALUE
rb_str_inspect(str)
VALUE str;
{
char *p, *pend;
VALUE result = rb_str_buf_new2("\"");
char s[5];
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;
while (p < pend) {
char c = *p++;
int len;
if (ismbchar(c) && p - 1 + (len = mbclen(c)) <= pend) {
rb_str_buf_cat(result, p - 1, len);
p += len - 1;
}
else if (c == '"'|| c == '\\' || (c == '#' && IS_EVSTR(p, pend))) {
s[0] = '\\'; s[1] = c;
rb_str_buf_cat(result, s, 2);
}
else if (ISPRINT(c)) {
s[0] = c;
rb_str_buf_cat(result, s, 1);
}
else if (c == '\n') {
s[0] = '\\'; s[1] = 'n';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\r') {
s[0] = '\\'; s[1] = 'r';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\t') {
s[0] = '\\'; s[1] = 't';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\f') {
s[0] = '\\'; s[1] = 'f';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\013') {
s[0] = '\\'; s[1] = 'v';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\010') {
s[0] = '\\'; s[1] = 'b';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\007') {
s[0] = '\\'; s[1] = 'a';
rb_str_buf_cat(result, s, 2);
}
else if (c == 033) {
s[0] = '\\'; s[1] = 'e';
rb_str_buf_cat(result, s, 2);
}
else {
sprintf(s, "\\%03o", c & 0377);
rb_str_buf_cat2(result, s);
}
}
rb_str_buf_cat2(result, "\"");
OBJ_INFECT(result, str);
return result;
} Related methods
- Instance methods
- %
- *
- +
- <<
- <=>
- ==
- =~
- []
- []=
- block_scanf
- bytes
- bytesize
- capitalize
- capitalize!
- casecmp
- center
- chars
- chomp
- chomp!
- chop
- chop
- chop!
- chop!
- concat
- count
- crypt
- delete
- delete
- delete!
- delete!
- downcase
- downcase!
- dump
- each
- each_byte
- each_char
- each_char
- each_line
- empty?
- end_regexp
- end_with?
- eql?
- gsub
- gsub!
- hash
- hex
- include?
- index
- initialize_copy
- insert
- inspect
- intern
- is_binary_data?
- is_complex_yaml?
- iseuc
- issjis
- isutf8
- jcount
- jlength
- jsize
- kconv
- length
- lines
- ljust
- lstrip
- lstrip!
- match
- mbchar?
- next
- next!
- oct
- partition
- replace
- reverse
- reverse!
- rindex
- rjust
- rpartition
- rstrip
- rstrip!
- scan
- scanf
- shellescape
- shellsplit
- size
- slice
- slice!
- split
- squeeze
- squeeze
- squeeze!
- squeeze!
- start_with?
- strip
- strip!
- sub
- sub!
- succ
- succ
- succ!
- succ!
- sum
- swapcase
- swapcase!
- to_f
- to_i
- to_s
- to_str
- to_sym
- to_yaml
- toeuc
- tojis
- tosjis
- toutf16
- toutf8
- tr
- tr
- tr!
- tr!
- tr_s
- tr_s
- tr_s!
- tr_s!
- unpack
- upcase
- upcase!
- upto
- Class methods
- new
- yaml_new
- Private methods
-
_expand_ch -
_regex_quote -
expand_ch_hash -
original_succ -
original_succ!