Forum: Ruby interpolation of escaped characters in strings

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eyal O. (Guest)
on 2007-02-20 20:10
(Received via mailing list)
Hi,

I've searched through the newsgroup, found some related stuff (about
'string interpolation at will') but couldn't quite figure it out
completely

My question: I read a string from file, using 'readlines', containing
special characters, especially \n. These characters are not
substituted into their binary control characters, e.g. my strings act
as if created with 'test\abc' instead of "test\nabc".   I would like
to convert these strings and have Ruby take account of the special
meaning of the escaped characters, so that I can print them out
nicely.
I see two ways:
- use gsub: str.gsub('\n',"\n"), but this solves only \n, so I need to
handle each possible escaped character manually
- use eval: eval('"' + str + '"'), but this is dangerous, for example
I had one string that ends with \, thereby breaking the eval code
since the slash escapes the closing quote.

Is there a better solution? I have the feeling that unpack would help,
but I simply can't understand the documentation of unpack.  I found
some discussions on str.interpolate, but I don't care for
interpolation of Ruby code, I only want replacement of these special
characters.

(I had the same problem for unicode escapes \uxxxx, but I solved that
with some unpack code from the web)
Xavier N. (Guest)
on 2007-02-20 20:23
(Received via mailing list)
On Feb 20, 2007, at 7:10 PM, Eyal O. wrote:

> to convert these strings and have Ruby take account of the special
> meaning of the escaped characters, so that I can print them out
> nicely.

Looks like you want something close to String#inspect.

   puts "foo\tbar\n".inspect # -> "foo\tbar\n"

-- fxn
Eyal O. (Guest)
on 2007-02-21 12:11
(Received via mailing list)
On Feb 20, 6:23 pm, Xavier N. <removed_email_address@domain.invalid> 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?
Harold H. (Guest)
on 2007-02-21 14:11
(Received via mailing list)
On 2/21/07, Eyal O. <removed_email_address@domain.invalid> wrote:
> 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'.
>

It looks like you're running into the fact that when you create a
string literal with a single quote (') it's treated differently than
when you construct the literal with double quotes...

Observe:
irb(main):001:0> a = 'test\nbreak'
=> "test\\nbreak"
irb(main):002:0> puts a
test\nbreak
=> nil
irb(main):003:0> puts 'whoops'
whoops
=> nil
irb(main):004:0> a = "test\n\break"
=> "test\n\break"
irb(main):005:0> puts a
test
reak
=> nil
irb(main):006:0> puts 'sweet!'
sweet!
=> nil

hth,
-Harold
Xavier N. (Guest)
on 2007-02-21 14:29
(Received via mailing list)
On Feb 21, 2007, at 1:10 PM, Harold H. wrote:

> when you construct the literal with double quotes...
Problem is he reads the string 'foo\nbar' from an external source,
that is, the string has a literal slash and a literal n within. He
wants a nice way to convert that and any other escape sequence into
an actual escape sequence.

-- fxn
Eyal O. (Guest)
on 2007-02-21 14:30
(Received via mailing list)
On 02/21/07/02/07 21:10 +0900, Harold H. wrote:
>It looks like you're running into the fact that when you create a
>string literal with a single quote (') it's treated differently than
>when you construct the literal with double quotes...
Yes. I know. My question is: how to convert the one variant ('') into
the
other variant ("") at runtime?

As I said in my first post, I can do that using eval or using gsub, but
both have problems. Does anybody know another way of converting
''-strings
into ""-ones?

 -eyal
Harold H. (Guest)
on 2007-02-21 16:27
(Received via mailing list)
On 2/21/07, Xavier N. <removed_email_address@domain.invalid> wrote:
> > It looks like you're running into the fact that when you create a
> > string literal with a single quote (') it's treated differently than
> > when you construct the literal with double quotes...
>
> Problem is he reads the string 'foo\nbar' from an external source,
> that is, the string has a literal slash and a literal n within. He
> wants a nice way to convert that and any other escape sequence into
> an actual escape sequence.
>

Dah!

I didn't even see that there were even earlier messages. I apologize.
What's worse, is that even after looking at this for 20+ mins, I still
don't see a way to cleanly do it. Once that backslash is escaped in
that string, you're pretty much hosed.

I think gsub is your best option here. Though, I'd love to to surprised.
(:

Apologies,
-Harold
unknown (Guest)
on 2007-02-21 18:18
(Received via mailing list)
On Wed, 21 Feb 2007, Eyal O. wrote:

> 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
Eyal O. (Guest)
on 2007-02-21 20:02
(Received via mailing list)
On 02/22/07/02/07 01:17 +0900, removed_email_address@domain.invalid wrote:
>         end
>       end.value
>     end
>   end
>
>seems like the obvious thing to so $SAFE is.
Thanks, I hadn't thought of using SAFE. My problem is that I encounter
not-wellformed strings in the data, e.g. 'hello\', which break
evaluation.
But now I catch the exception and just return the string un-evaluated,
which is fine, because those escape syntaxes are not allowed anyway.

So thanks, I've used your solution.

 -eyal
This topic is locked and can not be replied to.