[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

File Save Question

Tj Superfly

11/5/2007 9:49:00 PM

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-....

5 Answers

Drew Olson

11/5/2007 9:53:00 PM

0

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|
out << old_array.to_yaml
end

new_array = YAML.load(File.open("array.yaml"))

puts new_array.inspect

--
Posted via http://www.ruby-....

Michael Guterl

11/5/2007 10:53:00 PM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

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-....
>
>
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

7stud --

11/5/2007 11:28:00 PM

0

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
arr = []
end


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

require 'yaml'

begin
arr = YAML.load_file("my_array.yaml")

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

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

end

if !arr #then the file didn't exist
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|
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-....

John Joyce

11/6/2007 12:29:00 AM

0

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.


Tj Superfly

11/6/2007 12:37:00 AM

0

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

Thank you all for your help. :D
--
Posted via http://www.ruby-....