interpolation of escaped characters in strings

E

Eyal Oren

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)
 
X

Xavier Noria

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.

Looks like you want something close to String#inspect.

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

-- fxn
 
E

Eyal Oren

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?
 
H

Harold Hausman

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
 
X

Xavier Noria

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.

-- fxn
 
E

Eyal Oren

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
 
H

Harold Hausman

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
 
A

ara.t.howard

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
 
E

Eyal Oren

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top