removing quotes in a string

ircamsimac:~ cbarrie$ irb irb(main):001:0> string = '"This is a string"' => "\"This is a string\"" irb(main):002:0> string.gsub!('"', '') => "This is a string" irb(main):003:0>

Nono, those quote marks are simple there to show you that it is an object of the string type.

When that is printed to the screen they won't be there. Try this, in your controller create an intance variable like so

def index @string = "This is a string" end

and then in your index.rhtml view that corresponds to your controller

<%= @string %>

note how the string is written as "This is a string" but as: This is a string

So unless you defined your variable as

@string = ' "This is a string" ' # => NB the ' "

You wont have to gsub the quotes off. Just as array types are displayed in the terminal with on them strings are with "".

Hope this helps, Cam

Well that's more to do with now parsing the CSV correctly, it's now creating 2 strings for you. You should be returning "10/08/07", "hello world"

The Rails Recipes book has a great Recipe for that.

Something like CSV.parse( File.read("myfile.txt").gsub(/^"([^"]*)"$/, "\\1") ) might work. It should catch only rows beginning and ending with quotes, not containing quotes. If your quoted lines also contain escaped quotes like \", things get trickier.

If _all_ quotes should be removed, doing just gsub('"', '') as described in previous replies is easier.

Also worth taking a closer look at what creates the "sometimes". eg. are the files coming from different sources, or is the sometimes because you are using different code to read the file.

It is not always obvious what is coming from the content of the file and what is occurring from how you read it. Also, you may get differences depending on the file system or application creating the file.

If you open a file in a spreadsheet application to make some changes and then re-save it. This can introduce " around some fields, which may never actually have normally occurred.

Reading the file into a single string can sometimes be helpful

File.open("myfile.csv", "r") { |f|     @s = f.read   } you can then view the content directly on the console or use debug in the template

Using a file viewer can also be helpful. This one is for windows:

You can just run wnbrowse.exe directly from the zip file if like me you are reluctant to install things without trying them first. This can be a safer way to edit the file too.

Some thoughts that you may find useful Tonypm