From: Eric Hodel Date: 2006-12-11T10:24:14+09:00 Subject: Re: Question on IO objects, initialize, and yaml On Dec 10, 2006, at 12:19 , Mark Noworolski wrote: > Say I have a class with pseudo code as follows: > class Test > def initialize > @a=1 > @b=File.open("file+Time.now or similar") > end > end > > Now, imagine that I use this class for a while and then I want to > dump it to > a YAML file. > > When I restart my code, I want it to read in stuff from the YAML > file (if it > exists) and use that as a starting point to work from (basically, I am > saving state in the yaml file). You'll note that I cannot just do > this as > is, because reading in from YAML does not initialize the class, so > @b would > not be initialized correctly on restart. Ruby and YAML provide hooks to cause things to happen when you load from Marshal or YAML. I think #initialize_copy gets called for Marshal, I don't know the YAML ones, but they should be easy to find. > I'd like to just initialize the thing, then read in the YAML file to > overwrite the state with the saved values. Is there some extremely > obvious > ruby way to do this or something equivalent? Don't do this, use the built-in hooks, that's what they're for. -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net I LIT YOUR GEM ON FIRE!