require 'pp'
s='abc'
t='#{a}'
print s.pretty_inspect
print t.pretty_inspect
this outputs:
"abc"
"\#{a}"
This is of course correct - both show the content of the strings,
and both print it in a way that this can be used in an eval to get
the original string back:
eval('t1='+t.pretty_inspect.chomp) # ==> t1==t
My main objection is that from a viewpoint of readability, it would
be better if pretty_inspect would generate single quoted strings
instead of double quoted ones, since less \-escaping is necessary
in this case.
Since changing the present implementation of pretty_inspect might
break existing code, I suggest that
- either pretty_inspect takes an additional argument which
tells what type of string quotation shall be used for producing
the output string in the case of pretty-printing strings, or
- provide an additional function in pp (say: pretty_inspectq),
which formats strings by using single quotes.
The first variant would be more flexible (as we could then
even require 'abc'.pretty_inspect('%q()'), producing %q(abc)
in return), but causes more work. For the second variant,
the current code of pretty_inspect can be reused, changing
just the part which formats strings. Here is an implementation
suggestion for pretty_inspectq-ing a string s:
"'"+s.gsub(/([^\\]|^)'/,'\1\\\\'+"'")+"'"
Any opinions on this?
Ronald
···
--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162
--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162