From: Adam Shelly Date: 2005-09-07T03:26:30+09:00 Subject: Re: beginners YAML question On 9/2/05, daz wrote: > In short, @size is an attribute of Box ... but you're > using the inherited Array#to_yaml to serialise your object. > > Some pointers ... > > 1) By defining an initialize method in Box, you /override/ > the Array initialize - so, an empty Array is allocated. > To fix this, you need to call "super" (see 2) Right. > > 2) Array already has a size attribute, so this works: > 'size' was an extremely bad choice of attribute name. I was trying to reduce my complex real code to a simple example, but I oversimplified. > 3) Note that if you /don't/ inherit from Array... But I do want to inherit from Array... > > 4) If you want Box to behave like an Array but with > attributes of its own, there are ways. > Ask again (giving details) if you need help with > that or any other aspect. > That's exactly what I want. A "Box" object which acts like an array, but has other attributes too: For example: class Box< Array def initialize label, location, initial_size = 0, initial_value = nil, &block super initial_size, initial_value @label = label @loc = location end def location @loc end def move_to new_loc @loc = new_loc end def label @label end def look_at "This is a box with a label that says " + (@label || "_nothing_") end end box = Box.new("Books", "Hall Closet") puts box.look_at puts box.to_yaml box2 = YAML::load(box.to_yaml) puts box2.look_at puts "ERROR: Box not reloaded correctly" unless box2.label == box.label What I can't figure out is how to get the extra attributes in the YAML. Do I have to override the to_yaml method? Thanks for the help. -Adam