[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Double quote escape character

Alvaro Perez

8/16/2007 3:27:00 PM

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

11 Answers

Alvaro Perez

8/16/2007 3:38:00 PM

0

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

James Gray

8/16/2007 3:39:00 PM

0

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


Alex Young

8/16/2007 3:40:00 PM

0

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

Tom M

8/16/2007 3:41:00 PM

0

On Aug 16, 11:26 am, Alvaro Perez <alvaro.pmarti...@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 viahttp://www.ruby-....

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

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

Austin Ziegler

8/16/2007 3:43:00 PM

0

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

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 * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

James Gray

8/16/2007 3:43:00 PM

0

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

Harold Hausman

8/16/2007 3:44:00 PM

0

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

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

?,
-Harold

Alvaro Perez

8/16/2007 4:09:00 PM

0

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

Austin Ziegler

8/16/2007 4:21:00 PM

0

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
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Alvaro Perez

8/16/2007 4:29:00 PM

0

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