R
Ronald Fischer
require 'pp'
s=3D'abc'
t=3D'#{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=3D'+t.pretty_inspect.chomp) # =3D=3D> t1=3D=3Dt
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),=20
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
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
s=3D'abc'
t=3D'#{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=3D'+t.pretty_inspect.chomp) # =3D=3D> t1=3D=3Dt
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),=20
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
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162