On Wed, 21 Feb 2007, Eyal Oren wrote:

> On Feb 20, 6:23 pm, Xavier Noria <f... / hashref.com> wrote:
>> Looks like you want something close to String#inspect.
>>    puts "foo\tbar\n".inspect # -> "foo\tbar\n"
>
> no, that doesn't work. what I would like is that puts would actually
> print a tab and a newline, instead of \t and \n.
>
> E.g. a = 'test\nbreak'. Then "puts a.inspect" doesn't actually print a
> line break, it prints the  characters '\n'. I would like to transform
> "a" into a string that really includes a linebreak instead of the
> characters '\n'.
>
> Does anybody know how to do it, without eval or a manually crafted
> gsub?
>
>

why are you avoiding eval?

   harp:~ > cat a.rb
   class String
     def double_quote
       Thread.new do
         $SAFE = 12
         begin
           eval('"%s"' % self)
         rescue Exception => e
           e
         end
       end.value
     end
   end

   s = 'foo\tbar\nfoobar'
   p s
   p s.double_quote
   puts s.double_quote


   harp:~ > ruby a.rb
   "foo\\tbar\\nfoobar"
   "foo\tbar\nfoobar"
   foo     bar
   foobar


seems like the obvious thing to so

$SAFE is.

regards.

-a
-- 
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama