PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > File Save Question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
File Save Question

Réponse
 
LinkBack Outils de la discussion
Vieux 05/11/2007, 21h49   #1
nonstickglue@verizon.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut File Save Question

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 ful information.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 05/11/2007, 21h53   #2
olsonas@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Save Question

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-forum.com/.

  Réponse avec citation
Vieux 05/11/2007, 22h53   #3
mguterl@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Save Question

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 ful information.
> --
> Posted via http://www.ruby-forum.com/.
>
>

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

  Réponse avec citation
Vieux 05/11/2007, 23h27   #4
bbxx789_05ss@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Save Question

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-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 00h28   #5
dangerwillrobinsondanger@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Save Question

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.


  Réponse avec citation
Vieux 06/11/2007, 00h36   #6
nonstickglue@verizon.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File Save Question

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

Thank you all for your .
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h16.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13993 seconds with 14 queries