From: Mauricio Fernandez Date: 2006-12-12T09:07:40+09:00 Subject: Re: Question on IO objects, initialize, and yaml On Mon, Dec 11, 2006 at 02:43:22PM +0900, Mark Noworolski wrote: > On 12/10/06, Eric Hodel wrote: > > > >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 _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. [...] > 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 require 'yaml' class Foo def initialize @foo = 1 @bar = 2 end def yaml_initialize(tag, val) end end foo = Foo.new foo # => # YAML.load(YAML.dump(foo)) # => # class Foo def yaml_initialize(tag, val) # !> method redefined; discarding old yaml_initialize @foo, @bar = val.values_at("foo", "bar") end end YAML.load(YAML.dump(foo)) # => # -- Mauricio Fernandez - http://eigenclass.org - singular Ruby