From: Diego Virasoro Date: 2010-11-29T03:45:18+09:00 Subject: Re: Loading variables from a file [Noob Question] On Nov 28, 5:44 pm, Richard Mccormack wrote: > Hey everyone, > I've been working on some sort of a test-based game to learn ruby (and > programming in general :) and I got stuck with my saving/loading system. > To show you what I mean, this is what I used to save the game. (I want > the code to be as modular as I can, I have some sort of an addon system > planned...) > > def savegame(name, varstosave) >   savegame = File.new(name + ".tsl", "w") >      savegame.puts varstosave.to_s >      while varstosave.length != 0 >         savegame.puts varstosave[0] >         savegame.puts eval(varstosave[0]).to_s >         varstosave.delete_at(0) >      end >   savegame.close > end > > So, now I am trying to to load the variables from the file. I came up > with the start of a function: > > def loadgame(name, varstoload) >   puts "Loading..." >   loadgame = File.open(name + ".tsl", "r") >     while varstoload.length != 0 >         # ? >     end >   loadgame.close > end > > Where varstoload/varstosave is an array. Problem with the loading is > that I assumed there  was a way to take the first variable name from the > array and actually use it as a variable. So, if the array is > ["$intellect", "$strength"], I thought I would be able somehow to turn > a[0] into a variable object. It seems that there isn't a way to do this, > so I was wondering if there was a better way to manage the > saving/loading. >  - Thanks in advance! :) > > -- > Posted viahttp://www.ruby-forum.com/. Not sure if it helps, but here are a few tips. First, if you simply want to dump all the data in a file and get it back have a look at things like the YAML library: you can take your objects, and just with one line of code dump it into a file. (you can alternatively use Marshal but then the file will be written in binary format: great if you need to save space but not if you want to be able to open the file with a text editor to have a look). Second, and possibly more relevant. :) A common way to do what you are trying to do would be to read a line at a time from the file, analyse it and store the result. Here a couple of useful functions may be: File::read (it reads the whole file and returns a String object) File::readlines (it reads the whole file and returns an Array of String with one line of the file per element of the Array) Note that in both cases these are class methods, similar to the File.open you used. A maybe more elegant method, and more similar to what you were trying to do would be to use File#gets, which returns the "next" line. Then you could modify your code so: loadgame = File.open(name + ".tsl", "r") while !loadgame.eof? # i.e. until it reaches the end of file line = loadgame.gets # more code end loadgame.close Now, assuming you manage to get a line, you need to parse it to get the data you want. The most generic way would be to use a regexp, but for simple cases you can simply split the line into smaller Strings via the function String#split. This would make it easier to pick the name and the value. So far I've assumed that you want to load all the variables in the file. If you only want to load some, i.e. only those in the Array varstoload you can use Array#include, for example: loadgame = File.open(name + ".tsl", "r") while !loadgame.eof? # i.e. until it reaches the end of file line = loadgame.gets elements = line.split if varstoload.include? elements[0] # add elements[0] and elements[1] to some variable/object end end loadgame.close Hope that help. Feel free if you have other questions. Diego