Hi, does anyone know of a good way to remove leading the trailing quotes (") in a string please? Thanks Grace -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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> On Aug 16, 2:29 pm, Grace Xue <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, does anyone know of a good way to remove leading the trailing quotes > (") in a string please? > > Thanks > > Grace > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
cammo wrote:> 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>I''m a newbie and not sure if I understand how the '' and " and \ works. Supposed my string is str = "hello world" does it mean I should do str.gsub!(''"'','''') Would this remove all the other " in the middle of the string too? thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, Cam. Thanks for that. Though I''m trying to read a line out of a csv file which has leading and trailing quotes, therefore I can not put each supposed "column" inside the right place in the array. For example. The row should be 10/08/07,hello world So I put first column into date filed, and second column into description field. But sometimes it comes in as "10/08/07,hello world" Therefore, the usual parse[] doesnt work. Any suggestions? Cheers -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Aug 16, 3:44 pm, Grace Xue <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, Cam. Thanks for that. Though I''m trying to read a line out of a csv > file which has leading and trailing quotes, therefore I can not put each > supposed "column" inside the right place in the array. For example. > > The row should be > 10/08/07,hello world > So I put first column into date filed, and second column into > description field. > > But sometimes it comes in as > "10/08/07,hello world" > Therefore, the usual parse[] doesnt work. > > Any suggestions? > > Cheers > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Aug 16, 7:44 am, Grace Xue <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, Cam. Thanks for that. Though I''m trying to read a line out of a csv > file which has leading and trailing quotes, therefore I can not put each > supposed "column" inside the right place in the array. For example. > > The row should be > 10/08/07,hello world > So I put first column into date filed, and second column into > description field. > > But sometimes it comes in as > "10/08/07,hello world" > Therefore, the usual parse[] doesnt work. > > Any suggestions?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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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: http://www.ngthomas.co.uk/wnbrowse.html 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---