# How to Keep Format of Temporary File (How do I?)

**URL:** <https://www.ruby-forum.com/t/how-to-keep-format-of-temporary-file-how-do-i/143685>\
**Category:** Rails\
**Created:** [July 21, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/how-to-keep-format-of-temporary-file-how-do-i/143685 "2008-07-21T20:37:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 21, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/how-to-keep-format-of-temporary-file-how-do-i/143685/1 "2008-07-21T20:37:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 21, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/how-to-keep-format-of-temporary-file-how-do-i/143685/2 "2008-07-21T20:56:39Z")

</div>

On 21 Jul 2008, at 19:37, Joe P. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 21, 2008, 9:13pm UTC](https://www.ruby-forum.com/t/how-to-keep-format-of-temporary-file-how-do-i/143685/3 "2008-07-21T21:13:21Z")

</div>

Frederick C. wrote:

> You need to use something that will properly dump the structure of  
> your data, eg yaml or Marshal.dump
> 
> Fred

Thanks, 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]))
