Double quote escape character

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/.

This works in IRB:

'"' + string + '"'

Probably not the most elegant solution though.

···

On 8/16/07, Alvaro Perez <alvaro.pmartinez@gmail.com> 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 via http://www.ruby-forum.com/\.

--
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can't hear a word you're saying."

-Greg Graffin (Bad Religion)

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/.

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

···

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.

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

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.

···

On Aug 16, 11:26 am, Alvaro Perez <alvaro.pmarti...@gmail.com> wrote:

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

···

On 8/16/07, Alvaro Perez <alvaro.pmartinez@gmail.com> 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""

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

Sure there is:

   %Q{"hello"}

James Edward Gray II

···

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""

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

?,
-Harold

···

On 8/16/07, Alvaro Perez <alvaro.pmartinez@gmail.com> wrote:

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

""hello""

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/.

puts "\"hello\"" # => "hello"
puts "\"\"hello\"\"" # => ""hello""

p uses #inspect, which usually escapes certain values. IRB uses #inspect, too.

-austin

···

On 8/16/07, Alvaro Perez <alvaro.pmartinez@gmail.com> wrote:

It´s a bit strange this.

On my irb:

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

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

Austin Ziegler wrote:

···

On 8/16/07, Alvaro Perez <alvaro.pmartinez@gmail.com> 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/\.

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.

···

On Aug 16, 10:28 am, Alvaro Perez <alvaro.pmarti...@gmail.com> wrote:

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