From: Mark Noworolski Date: 2006-12-11T14:43:22+09:00 Subject: Re: Question on IO objects, initialize, and yaml ------=_Part_75564_13885918.1165815800021 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline I _want_ to use a built-in hook (the ruby way, if you will), if I can figure out which one and how to use it. Based on your suggestion, I've googled and found this code: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/104402 Now, I've experimented with initialize_copy on this code and your recommendation, and I cannot get it to do what I want. Anybody got any other suggestions, or a concrete example for me? mark On 12/10/06, Eric Hodel wrote: > > 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! > > > ------=_Part_75564_13885918.1165815800021--