From: "Dan Stevens (IAmAI)" Date: 2007-08-07T18:17:36+09:00 Subject: Re: Feature request (pretty_inspect variant, yielding single quotes instead of double) Here's what I suggest: class String def quote(str="\'") "#{str}#{self}#{str}" end def quote!(str="\'") self.insert(0, str) << str end end #Tests test = "abc" => "abc" test.quote => "'abc'" test.quote("\"") => "\"abc\"" test.quote("123") => "123abc123" test => "abc" test.quote! => "'abc'" test => "'abc'" On 07/08/07, Ronald Fischer wrote: > 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 > Phone: +49-89-452133-162 > > -- > Ronald Fischer > Phone: +49-89-452133-162 > >