Double quote escape character

Discussion in 'Ruby' started by Alvaro Perez, Aug 16, 2007.

  1. Alvaro Perez

    Alvaro Perez Guest

    Hi all,

    I'm trying to export a string to a excel file using fastercsv and I
    wanted it double quote to make it safer. So i tried to use the escape
    character \" to insert double quotes but it doesn't work:

    - string produces string with no quotes in the file

    - "\"" << string << "\"" produces """string"""

    - "\"" + string + "\"" produces """string"""

    I have no idea on how to solve this.


    Thanks for the help,
    Alvaro.
    --
    Posted via http://www.ruby-forum.com/.
     
    Alvaro Perez, Aug 16, 2007
    #1
    1. Advertising

  2. Alvaro Perez

    Alvaro Perez Guest

    Hi all,

    I think there is not problem at all as it seems that the FasterCSV class
    is the one who's erasing the double quotes while converting the strings
    to cvs.

    Anyway, it's still interesting to notice that there's no possible way to
    write in Ruby a string like this:

    ""hello""


    Regards,
    Alvaro.
    --
    Posted via http://www.ruby-forum.com/.
     
    Alvaro Perez, Aug 16, 2007
    #2
    1. Advertising

  3. On Aug 16, 2007, at 10:26 AM, Alvaro Perez wrote:

    > I'm trying to export a string to a excel file using fastercsv and I
    > wanted it double quote to make it safer.


    FasterCSV handles all the quoting for you. That's why you use it.
    So you should just be doing something like:

    fcsv << %w[array of fields for row here]

    James Edward Gray II
     
    James Edward Gray II, Aug 16, 2007
    #3
  4. Alvaro Perez

    Alex Young Guest

    Alvaro Perez wrote:
    > Hi all,
    >
    > I'm trying to export a string to a excel file using fastercsv and I
    > wanted it double quote to make it safer. So i tried to use the escape
    > character \" to insert double quotes but it doesn't work:
    >
    > - string produces string with no quotes in the file
    >
    > - "\"" << string << "\"" produces """string"""
    >
    > - "\"" + string + "\"" produces """string"""
    >
    > I have no idea on how to solve this.

    You don't need to quote your output - FasterCSV quotes it iff necessary.
    Try sticking a '"' in the middle of your string and see what it does.

    --
    Alex
     
    Alex Young, Aug 16, 2007
    #4
  5. Alvaro Perez

    Guest

    On Aug 16, 11:26 am, Alvaro Perez <> wrote:
    > Hi all,
    >
    > I'm trying to export a string to a excel file using fastercsv and I
    > wanted it double quote to make it safer. So i tried to use the escape
    > character \" to insert double quotes but it doesn't work:
    >
    > - string produces string with no quotes in the file
    >
    > - "\"" << string << "\"" produces """string"""
    >
    > - "\"" + string + "\"" produces """string"""
    >
    > I have no idea on how to solve this.
    >
    > Thanks for the help,
    > Alvaro.
    > --
    > Posted viahttp://www.ruby-forum.com/.


    try this:
    irb
    > str = "foo"
    > str2 = %Q{#{str}}
    > p str2


    I think that's what you're looking for.
     
    , Aug 16, 2007
    #5
  6. On 8/16/07, Alvaro Perez <> wrote:
    > Hi all,
    >
    > I think there is not problem at all as it seems that the FasterCSV class
    > is the one who's erasing the double quotes while converting the strings
    > to cvs.
    >
    > Anyway, it's still interesting to notice that there's no possible way to
    > write in Ruby a string like this:
    >
    > ""hello""


    Sure there is.

    '"hello"'
    '""hello""'
    "\"hello\""
    "\"\"hello\"\""
    %{"hello"}
    %{""hello""}
    <<-EOS
    "hello"
    ""hello""
    EOS

    (Okay, that last one actually makes a multi-line string, but the point
    remains the same.)

    -austin
    --
    Austin Ziegler * * http://www.halostatue.ca/
    * * http://www.halostatue.ca/feed/
    *
     
    Austin Ziegler, Aug 16, 2007
    #6
  7. On Aug 16, 2007, at 10:37 AM, Alvaro Perez wrote:

    > Anyway, it's still interesting to notice that there's no possible
    > way to
    > write in Ruby a string like this:
    >
    > ""hello""


    Sure there is:

    %Q{"hello"}

    James Edward Gray II
     
    James Edward Gray II, Aug 16, 2007
    #7
  8. On 8/16/07, Alvaro Perez <> wrote:
    > Anyway, it's still interesting to notice that there's no possible way to
    > write in Ruby a string like this:
    >
    > ""hello""
    >


    irb(main):001:0> puts "\"\"hello\"\""
    ""hello""
    => nil

    ?,
    -Harold
     
    Harold Hausman, Aug 16, 2007
    #8
  9. Alvaro Perez

    Alvaro Perez Guest

    It´s a bit strange this.

    On my irb:

    irb(main):053:0> p "\"hello\""
    "\"hello\""

    irb(main):049:0> h = '""hello""'
    => "\"\"hello\"\""

    irb(main):044:0> string = "hello"
    => "hello"
    irb(main):046:0> %Q{#{string}}
    => "hello"
    irb(main):047:0> p string
    "hello"

    irb(main):048:0> puts "\"\"hello\"\""
    ""hello""


    I´m not pretty sure, but the last one it´s the only that seems to
    produce exactly what i was looking for...

    Because "\"\"hello\"\"" != ""hello"" right? or it is altough irb shows
    it in different ways?
    --
    Posted via http://www.ruby-forum.com/.
     
    Alvaro Perez, Aug 16, 2007
    #9
  10. On 8/16/07, Alvaro Perez <> wrote:
    > It=B4s a bit strange this.
    >
    > On my irb:
    >
    > irb(main):053:0> p "\"hello\""
    > "\"hello\""


    puts "\"hello\"" # =3D> "hello"
    puts "\"\"hello\"\"" # =3D> ""hello""

    p uses #inspect, which usually escapes certain values. IRB uses #inspect, t=
    oo.

    -austin
    --=20
    Austin Ziegler * * http://www.halostatue.ca/
    * * http://www.halostatue.ca/feed/
    *
     
    Austin Ziegler, Aug 16, 2007
    #10
  11. Alvaro Perez

    Alvaro Perez Guest

    Austin Ziegler wrote:
    > On 8/16/07, Alvaro Perez <> wrote:
    >> It�s a bit strange this.
    >>
    >> On my irb:
    >>
    >> irb(main):053:0> p "\"hello\""
    >> "\"hello\""

    >
    > puts "\"hello\"" # => "hello"
    > puts "\"\"hello\"\"" # => ""hello""
    >
    > p uses #inspect, which usually escapes certain values. IRB uses
    > #inspect, too.
    >
    > -austin


    that´s very interesting, I had already notice that p and puts work
    differently, i'll search about that #inspect thing...

    --
    Posted via http://www.ruby-forum.com/.
     
    Alvaro Perez, Aug 16, 2007
    #11
  12. Alvaro Perez

    Phrogz Guest

    On Aug 16, 10:28 am, Alvaro Perez <> wrote:
    > that´s very interesting, I had already notice that p and puts work
    > differently, i'll search about that #inspect thing...


    Let me try to explain this:
    @a = "foo"
    @a is now a 3-character string (not 5 characters; the double quotes
    aren't part of the string).

    puts @a.length
    #=> 3

    puts @a, @a.inspect
    #=> foo
    #=> "foo"

    p @a
    #=> "foo"

    As Austin said, the 'p' method calls the #inspect method on its
    arguments. Inspect tries to show you what type an object is, so (in
    the above) it puts double-quotes around the 3 characters in @a to show
    you that it's a string.

    irb also uses #inspect to show you the value of the last expression on
    the line.
    irb(main):001:0> @a = "foo"
    => "foo"

    See? Those double quotes aren't part of the actual string, they're
    just there to show you what's inside the string.

    Now, let's create a string that has a double quote character inside
    it.
    @b = 'foo"bar'
    puts @b, @b.length, @b.inspect
    #=> foo"bar
    #=> 7
    #=> "foo\"bar"

    The string is exactly 7 characters long. When you call #inspect, it
    puts double quotes around the string when showing it to you. And, so
    that you know that the double quote in the middle isn't the end of the
    string, it shows you a source-code-like representation, a backslash
    before the double quote.

    This same confusion is seen in this thread already. When you wrote:
    ...there's no possible way to write in Ruby a string like this:
    ""hello""
    what did you mean? A 7 character string with double quotes at either
    end? Or a 9 character string with two double quotes at each end?

    As Austin showed, there are plenty of ways to create either. You just
    need to not confuse the contents of the string what irb and #inspect
    are showing you in their attempt to be clear about the contents of the
    string.
     
    Phrogz, Aug 16, 2007
    #12
    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:
    3
    Views:
    13,240
  2. Eric Layman
    Replies:
    3
    Views:
    630
    Rad [Visual C# MVP]
    Apr 14, 2007
  3. Sanjeev
    Replies:
    3
    Views:
    1,720
    Andrea Francia
    May 3, 2008
  4. Evan
    Replies:
    3
    Views:
    522
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  5. Alden Streeter

    Javascript double quote escape character?

    Alden Streeter, Nov 24, 2003, in forum: ASP General
    Replies:
    1
    Views:
    288
    Alden Streeter
    Nov 26, 2003
Loading...

Share This Page