# File Save Question

**URL:** <https://rubytalk.org/t/file-save-question/41931>\
**Category:** ruby-talk\
**Created:** [5 November 2007 21:49 UTC](https://rubytalk.org/t/file-save-question/41931 "2007-11-05T21:49:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![12345678\_123456789](https://avatars.discourse-cdn.com/v4/letter/1/3ec8ea/32.png) [@12345678\_123456789](https://rubytalk.org/u/12345678_123456789)\
**Post date:** [5 November 2007 21:49 UTC](https://rubytalk.org/t/file-save-question/41931/1 "2007-11-05T21:49:23Z")

</div>

Hello!

I've created a program that will run and update the array storing the  
information within that program. When the program is over, I would like  
the file to save, so th next time I run it, the same information is in  
the array and it will be able to start back where it left off; if that  
makes sense.

I've tried google-ing it, but can't find any helpful information.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Drew\_Olson](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Drew\_Olson](https://rubytalk.org/u/Drew_Olson)\
**Post date:** [5 November 2007 21:53 UTC](https://rubytalk.org/t/file-save-question/41931/2 "2007-11-05T21:53:11Z")

</div>

Tj Superfly wrote:

> Hello!
> 
> I've created a program that will run and update the array storing the  
> information within that program. When the program is over, I would like  
> the file to save, so th next time I run it, the same information is in  
> the array

old\_array = [1,2,3,4]

File.open("array.yaml","w") do |out|  
&nbsp;&nbsp;out \<\< old\_array.to\_yaml  
end

new\_array = YAML.load(File.open("array.yaml"))

puts new\_array.inspect

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Michael\_Guterl1](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Michael\_Guterl1](https://rubytalk.org/u/Michael_Guterl1)\
**Post date:** [5 November 2007 22:53 UTC](https://rubytalk.org/t/file-save-question/41931/3 "2007-11-05T22:53:11Z")

</div>

Try googling "ruby serialization" that will give you more information on the  
topic. The two main classes you want to look at are Marshal and YAML.

Brief synopsis:  
Marshal is fast, but unreadable, and is not guaranteed to work across  
different versions of ruby.

YAML is human readable, works across all versions of ruby and even other  
programming languages that support it.

Michael Guterl

> **···**
>
> On 11/5/07, Tj Superfly \<nonstickglue@verizon.net\> wrote:
> 
> > Hello!
> > 
> > I've created a program that will run and update the array storing the  
> > information within that program. When the program is over, I would like  
> > the file to save, so th next time I run it, the same information is in  
> > the array and it will be able to start back where it left off; if that  
> > makes sense.
> > 
> > I've tried google-ing it, but can't find any helpful information.  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [5 November 2007 23:27 UTC](https://rubytalk.org/t/file-save-question/41931/4 "2007-11-05T23:27:34Z")

</div>

Tj Superfly wrote:

> Hello!
> 
> I've created a program that will run and update the array storing the  
> information within that program. When the program is over, I would like  
> the file to save, so th next time I run it, the same information is in  
> the array and it will be able to start back where it left off; if that  
> makes sense.

You can't. You have to save the data to a file and then read the data  
back from the file. One approach would be to write each item in the  
array to a file. Then, the next time your program runs, you could read  
each item back in from the file and insert it in an array. However,  
each item you read back will be a string, so if the original item was a  
number, you have to convert to the string back to a number. That's kind  
of a hassle to do all that.

The ruby standard library yaml will do that all for you. It allows you  
to write the whole array to the file and then read the whole array back  
in one shot.

One problem you are going to encounter is what to do when you run the  
program the first time. You can either create the file beforehand and  
leave it blank, e.g.

$touch my\_array.yaml #creates a blank file

and then do this:

arr = YAML.load\_file("my\_array.yaml")

if !arr  
&nbsp;&nbsp;arr =   
end

Or, you can have your program create the file if it doesn't exist,  
something like this:

require 'yaml'

begin  
&nbsp;&nbsp;arr = YAML.load\_file("my\_array.yaml")

rescue Errno::ENOENT #error type is from the error message  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#when you open a file that doesn't  
exit

&nbsp;&nbsp;#The first time the program is run you will  
&nbsp;&nbsp;#get an error saying the file doesn't exist  
&nbsp;&nbsp;#which will make execution jump into here.  
&nbsp;&nbsp;#By putting this rescue block here,  
&nbsp;&nbsp;#the error won't cause your program to  
&nbsp;&nbsp;#terminate.

end

if !arr #then the file didn't exist  
&nbsp;&nbsp;arr =   
end

p arr

#Your program then adds items to arr, e.g.:  
arr \<\< 10 \<\< 12.5 \<\< "hello"

#At end of program, write array to file:  
File.open("my\_array.yaml", "w") do |file|  
&nbsp;&nbsp;YAML.dump(arr, file)  
end

If you run that a few times, you will see that the data keeps getting  
added to the array.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [6 November 2007 00:28 UTC](https://rubytalk.org/t/file-save-question/41931/5 "2007-11-06T00:28:33Z")

</div>

If you don't want to use YAML or other methods,  
you can also save the array's data in an even simpler format, like CSV (comma separated values)  
It can be a lot simpler and can be sent of to other apps like Excel or whatever.  
Just another option.  
You can then load it back from the CSV file into an array.

---

<div class="post-metadata">

**Author:** ![12345678\_123456789](https://avatars.discourse-cdn.com/v4/letter/1/3ec8ea/32.png) [@12345678\_123456789](https://rubytalk.org/u/12345678_123456789)\
**Post date:** [6 November 2007 00:36 UTC](https://rubytalk.org/t/file-save-question/41931/6 "2007-11-06T00:36:37Z")

</div>

I seem to have everything working with the YAML, so all is good.

Thank you all for your help. 😃

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
