Double quote escape character

A

Alvaro Perez

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

Alvaro Perez

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

James Edward Gray II

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
 
A

Alex Young

Alvaro said:
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.
 
T

thomas.macklin

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.

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

I think that's what you're looking for.
 
A

Austin Ziegler

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
 
J

James Edward Gray II

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
 
H

Harold Hausman

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
 
A

Alvaro Perez

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

Alvaro Perez

Austin said:
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...
 
P

Phrogz

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top