interpolation of escaped characters in strings

Discussion in 'Ruby' started by Eyal Oren, Feb 20, 2007.

  1. Eyal Oren

    Eyal Oren Guest

    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)
    Eyal Oren, Feb 20, 2007
    #1
    1. Advertising

  2. Eyal Oren

    Xavier Noria Guest

    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
    Xavier Noria, Feb 20, 2007
    #2
    1. Advertising

  3. Eyal Oren

    Eyal Oren Guest

    On Feb 20, 6:23 pm, Xavier Noria <> 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?
    Eyal Oren, Feb 21, 2007
    #3
  4. On 2/21/07, Eyal Oren <> 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
    Harold Hausman, Feb 21, 2007
    #4
  5. Eyal Oren

    Xavier Noria Guest

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

    > On 2/21/07, Eyal Oren <> 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
    Xavier Noria, Feb 21, 2007
    #5
  6. Eyal Oren

    Eyal Oren Guest

    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
    Eyal Oren, Feb 21, 2007
    #6
  7. On 2/21/07, Xavier Noria <> wrote:
    > On Feb 21, 2007, at 1:10 PM, Harold Hausman wrote:
    >
    > > On 2/21/07, Eyal Oren <> 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
    Harold Hausman, Feb 21, 2007
    #7
  8. Eyal Oren

    Guest

    On Wed, 21 Feb 2007, Eyal Oren wrote:

    > On Feb 20, 6:23 pm, Xavier Noria <> 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
    , Feb 21, 2007
    #8
  9. Eyal Oren

    Eyal Oren Guest

    On 02/22/07/02/07 01:17 +0900, 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
    Eyal Oren, Feb 21, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    754
  2. Ben Finney
    Replies:
    2
    Views:
    334
    Josiah Carlson
    Apr 3, 2004
  3. Henrik S. Hansen

    Translating escaped characters

    Henrik S. Hansen, Nov 1, 2004, in forum: Python
    Replies:
    2
    Views:
    284
    Henrik S. Hansen
    Nov 2, 2004
  4. Derek Basch
    Replies:
    2
    Views:
    449
    Derek Basch
    Mar 25, 2005
  5. Thea
    Replies:
    2
    Views:
    618
Loading...

Share This Page