From: Robert Klemme Date: 2007-08-07T17:59:29+09:00 Subject: Re: Feature request (pretty_inspect variant, yielding single quotes instead of double) 2007/8/7, Ronald Fischer : > 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? You would have to also explain how embedded control characters will be printed because there is no way I know of at the moment that would allow to embed e.g. a newline in a single quote string: irb(main):006:0> "\n" => "\n" irb(main):007:0> '\n' => "\\n" irb(main):009:0> "\064" => "4" irb(main):010:0> '\064' => "\\064" If you introduce a new syntax then the output of pp becomes inconsistent with what Ruby normally understands to be single quoted strings. I believe you would have to have better reasons than readability to introduce this inconsistency and make pp's output less useful in terms of reuse (copy and paste). Personally I doubt it is worthwhile. Btw, there is another variant for switching between single and double quotes: you can use a global variable. This has the advantage that you do not need to touch any #inspect implementation of custom classes - but comes at a price of course. Kind regards robert