Frederick Cheung
2008-Jul-21 18:56 UTC
Re: How to Keep Format of Temporary File (How do I?)
On 21 Jul 2008, at 19:37, Joe Peck wrote:> > Hi, > > Alright, here is the problem. In one action, I store an array of > arrays > in a temporary file, like this: > > @temp_file = Tempfile.new("temporary_csv_file") > @temp_file.puts @parsed_file > @temp_file.close > > By array of arrays, I mean something like this: > [["ihoih", "iuhoi", "hibib"] ,["ihdfd", "eefoi", "qwib"], ["jmjih", > "plplhoi", "recsf"]] > > In another action, I try to read the file, like this (path is the path > of the file): > @stored_file = File.open(params[:path]) > > But I just get back a String with each value separated by \n > > How can I store and retrieve an array of arrays using a temporary > file?#Have you looked at what happens when you do puts on an array ? Puts isn''t some magic serializer, what it''s outputting to your file isn''t enough for anyone to put it back together. You need to use something that will properly dump the structure of your data, eg yaml or Marshal.dump Fred> > -- > 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> You need to use something that will properly dump the structure of > your data, eg yaml or Marshal.dump > > FredThanks, I got it working. Hopefully this can help anyone else searching with the same problem. @temp_file = Tempfile.new("temporary_csv_file") @temp_file.write(YAML::dump(@parsed_file)) @temp_file.close Then to open it: @csv_file = YAML::load(File.open(params[:path])) -- 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 -~----------~----~----~----~------~----~------~--~---