[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

interpolation of escaped characters in strings

Eyal Oren

2/20/2007 6:07:00 PM

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)

8 Answers

Xavier Noria

2/20/2007 6:23:00 PM

0


On Feb 20, 2007, at 7:10 PM, Eyal Oren wrote:

> 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


Eyal Oren

2/21/2007 10:08:00 AM

0

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?

Harold Hausman

2/21/2007 12:10:00 PM

0

On 2/21/07, Eyal Oren <eyal.oren@gmail.com> 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 Noria

2/21/2007 12:29:00 PM

0

On Feb 21, 2007, at 1:10 PM, Harold Hausman wrote:

> On 2/21/07, Eyal Oren <eyal.oren@gmail.com> 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...

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 Oren

2/21/2007 12:30:00 PM

0

On 02/21/07/02/07 21:10 +0900, Harold Hausman 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 Hausman

2/21/2007 2:27:00 PM

0

On 2/21/07, Xavier Noria <fxn@hashref.com> wrote:
> On Feb 21, 2007, at 1:10 PM, Harold Hausman wrote:
>
> > On 2/21/07, Eyal Oren <eyal.oren@gmail.com> 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...
>
> 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

Ara.T.Howard

2/21/2007 4:18:00 PM

0

Eyal Oren

2/21/2007 6:02:00 PM

0

On 02/22/07/02/07 01:17 +0900, ara.t.howard@noaa.gov wrote:
>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